Skip to content

Commit

Permalink
Update README.md documentation
Browse files Browse the repository at this point in the history
Adds a mention and an example of default values for attributes.
  • Loading branch information
ljuti authored Dec 18, 2023
1 parent b397acc commit 66b3292
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ If we try to pass an invalid value to the initializer, we’ll get an error. Und
def first_name = @name.split(/\s/).first
```

Attributes can have default values.

```ruby
class Task
extend Literal::Attributes

attribute :timeout, Integer, default: 60
attribute :options, Hash, default: proc { { notify_on_completion: true } }
attribute :states, Array, default ["initialized", "started", "finished"].freeze
end
```

Defaults must be immutable so strings, hashes and arrays must be either frozen or wrapped in a Proc.

The type-checking is really designed for the public interface. Internally, I think it’s good practice to reference the instance variables directly. You can always use the writers (which are private by default) if you need to do type-checking, but that’s not usually necessary. We’re not trying to make the application type-safe. We can’t do that without a complete type system. What we’re doing is adding some helpful checks to the main public interfaces.

## `Literal::Enum`
Expand Down

0 comments on commit 66b3292

Please sign in to comment.