Module: StronglyTyped::Attributes
- Defined in:
- lib/strongly_typed/attributes.rb
Instance Method Summary collapse
-
#attribute(name, type = Object) ⇒ Object
Create attribute accesors for the included class Also validations and coercions for the type specified.
-
#attributes ⇒ Hash
Memoized hash storing keys for names & values for types pairs.
-
#members ⇒ Array<Symbol>
Returns the attribute names created with attribute().
Instance Method Details
#attribute(name, type = Object) ⇒ Object
Create attribute accesors for the included class
Also validations and coercions for the type specified
22 23 24 25 26 27 28 29 30 |
# File 'lib/strongly_typed/attributes.rb', line 22 def attribute(name, type=Object) name = name.to_sym #normalize raise NameError, "attribute `#{name}` already created" if members.include?(name) raise TypeError, "second argument, type, must be a Class but got `#{type.inspect}` insted" unless type.is_a?(Class) raise TypeError, "directly converting to Bignum is not supported, use Integer instead" if type == Bignum new_attribute(name, type) end |
#attributes ⇒ Hash
Memoized hash storing keys for names & values for types pairs
35 36 37 |
# File 'lib/strongly_typed/attributes.rb', line 35 def attributes @attributes ||= {} end |
#members ⇒ Array<Symbol>
Returns the attribute names created with attribute()
42 43 44 |
# File 'lib/strongly_typed/attributes.rb', line 42 def members attributes.keys end |