Class: AttributesDSL::Attributes Private
- Inherits:
-
Object
- Object
- AttributesDSL::Attributes
- Defined in:
- lib/attributes_dsl/attributes.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Describes a collection of attributes declaration with methods to validate and extract instance attributes from a hash.
Instance Attribute Summary collapse
-
#attributes ⇒ Set
readonly
private
Uses the set of attributes to ensure their uniqueness (by name).
Instance Method Summary collapse
-
#extract(input) ⇒ Hash
private
Extracts instance attributes from the input hash.
-
#initialize(attributes = {}) ⇒ Attributes
constructor
private
Initializes an immutable collection with an initial set of attributes.
-
#register(name, options = {}, &coercer) ⇒ AttributesDSL::Attributes
private
Initializes the attribute from given arguments and returns new immutable collection with the attribute.
Constructor Details
#initialize(attributes = {}) ⇒ Attributes
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes an immutable collection with an initial set of attributes
26 27 28 29 |
# File 'lib/attributes_dsl/attributes.rb', line 26 def initialize(attributes = {}) @attributes = attributes IceNine.deep_freeze(self) end |
Instance Attribute Details
#attributes ⇒ Set (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Uses the set of attributes to ensure their uniqueness (by name)
20 21 22 |
# File 'lib/attributes_dsl/attributes.rb', line 20 def attributes @attributes end |
Instance Method Details
#extract(input) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extracts instance attributes from the input hash
Assigns default values and uses coercions when applicable.
52 53 54 55 56 57 58 |
# File 'lib/attributes_dsl/attributes.rb', line 52 def extract(input) validate(input).inject({}) do |a, e| key = e.name value = input.fetch(key) { e.default } a.merge(key => e.value(value)) end end |
#register(name, options = {}, &coercer) ⇒ AttributesDSL::Attributes
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the attribute from given arguments and returns new immutable collection with the attribute
38 39 40 41 42 |
# File 'lib/attributes_dsl/attributes.rb', line 38 def register(name, = {}, &coercer) self.class.new( attributes.merge(name => Attribute.new(name, , &coercer)) ) end |