Class: Alphred::Struct::Attribute
- Inherits:
-
Object
- Object
- Alphred::Struct::Attribute
- Defined in:
- lib/alphred/struct.rb
Instance Attribute Summary collapse
-
#coerce ⇒ Object
readonly
Returns the value of attribute coerce.
-
#enum ⇒ Object
readonly
Returns the value of attribute enum.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, **options) ⇒ Attribute
constructor
A new instance of Attribute.
- #required? ⇒ Boolean
- #value_from(hash) ⇒ Object
Constructor Details
#initialize(name, **options) ⇒ Attribute
Returns a new instance of Attribute.
52 53 54 55 56 57 |
# File 'lib/alphred/struct.rb', line 52 def initialize(name, **) @name = name @required = .fetch(:required, false) @coerce = .fetch(:coerce, ->(x) { x }) @enum = [:enum] end |
Instance Attribute Details
#coerce ⇒ Object (readonly)
Returns the value of attribute coerce.
50 51 52 |
# File 'lib/alphred/struct.rb', line 50 def coerce @coerce end |
#enum ⇒ Object (readonly)
Returns the value of attribute enum.
50 51 52 |
# File 'lib/alphred/struct.rb', line 50 def enum @enum end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
50 51 52 |
# File 'lib/alphred/struct.rb', line 50 def name @name end |
Instance Method Details
#required? ⇒ Boolean
59 60 61 |
# File 'lib/alphred/struct.rb', line 59 def required? !!@required end |
#value_from(hash) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/alphred/struct.rb', line 63 def value_from(hash) uncoerced = hash.fetch(name) { raise RequiredAttributeError.new(name) } coerced = coerce.call(uncoerced) raise InvalidEnumError.new(coerced) if enum && !enum.include?(coerced) { name => coerced } end |