Skip to content

Commit

Permalink
Move from classes to structs (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephendolan authored Apr 13, 2021
1 parent 02b63c7 commit 8308ffd
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 22 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Deploy documentation

on:
push:
branches:
- main
workflow_dispatch:
release:
types: [published]

jobs:
deploy:
Expand All @@ -21,7 +21,7 @@ jobs:
run: shards install --ignore-crystal-version

- name: "Generate docs"
run: crystal docs
run: crystal docs --project-name=Decorator --project-version=${GITHUB_REF##*/}

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ jobs:

runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental }}
container: crystallang/crystal:${{ matrix.crystal_version }}-alpine

steps:
- uses: actions/checkout@v2

- uses: oprypin/install-crystal@v1
with:
crystal: ${{matrix.crystal_version}}

- name: Check format
run: crystal tool format --check

Expand All @@ -49,7 +52,7 @@ jobs:
if: steps.crystal-cache.outputs.cache-hit != 'true'
run: shards check || shards install --ignore-crystal-version

- name: Crystal Ameba Linter
- name: Run Ameba static code analysis
run: ./bin/ameba

- name: Run tests
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ Run `shards install`

## 🎬 Screencast

<div style="width:50%">
<a href="https://luckycasts.com/videos/decorator-shard"><img src="https://i.imgur.com/1gs4z0c.jpg" title="Decorating Lucky Apps with the Decorator Shard" /></a>
</div>
<a href="https://luckycasts.com/videos/decorator-shard"><img src="https://i.imgur.com/1gs4z0c.jpg" title="Decorating Lucky Apps with the Decorator Shard" width="500" /></a>

[Watch Screencast](https://luckycasts.com/videos/decorator-shard)
[Watch a LuckyCast on the Decorator shard](https://luckycasts.com/videos/decorator-shard)

## Usage

Expand All @@ -34,10 +32,10 @@ Require the shard:
require "decorator"
```

Create a decorator that inherits from Decorator::Base, and tell the decorator what it `decorates` to decorate:
Create a decorator `Struct` that inherits from `Decorator::Base`, and define what it `decorates`:

```crystal
class TimeDecorator < Decorator::Base
struct TimeDecorator < Decorator::Base
decorates time : Time
# Return a pretty version of a date and weekday in the format:
Expand All @@ -54,7 +52,7 @@ class TimeDecorator < Decorator::Base
end
```

In the above example, `Decorator` adds the following to the `TimeDecorator` class for you:
In the above example, `Decorator` adds the following to the `TimeDecorator` struct for you:

- An `initialize(@time : Time)` method
- A `getter` (or `getter?` for `Bool` types) for `@time`
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: decorator
description: A simple object decoration library for Crystal apps
version: 0.3.0
version: 1.0.0

authors:
- Stephen Dolan <stephen@luckycasts.com>
Expand Down
2 changes: 1 addition & 1 deletion spec/decorator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class User
end
end

class UserDecorator < Decorator::Base
struct UserDecorator < Decorator::Base
decorates user : User

def full_name
Expand Down
1 change: 1 addition & 0 deletions src/decorator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ require "./decorator/*"

# Decorator provides a simple interface to decorate an object with additional functionality.
module Decorator
VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }}
end
8 changes: 4 additions & 4 deletions src/decorator/base.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Decorator::Base
abstract struct Decorator::Base
# Keep track of how many `decorates` statements have been provided so that we can limit to 1.
macro inherited
DECORATOR_ASSIGNS = [] of Nil
Expand All @@ -8,15 +8,15 @@ class Decorator::Base
#
# Currently, you can only supply **one** `decorates` statement per decorator that inherits from `Decorator::Base`.
#
# Given a decorator class like this:
# Given a decorator struct like this:
#
# ```
# class TimeDecorator < Decorator::Base
# struct TimeDecorator < Decorator::Base
# decorates time : Time
# end
# ```
#
# The following items are made available to the `TimeDecorator` class:
# The following items are made available to the `TimeDecorator` struct:
#
# - An `initialize(@time : Time)` method
# - A `getter` (or `getter?` for `Bool` types) for `@time`
Expand Down
3 changes: 0 additions & 3 deletions src/decorator/version.cr

This file was deleted.

0 comments on commit 8308ffd

Please sign in to comment.