Module: Stannum::Entities::Attributes
- Included in:
- Stannum::Entity, Struct
- Defined in:
- lib/stannum/entities/attributes.rb
Overview
Methods for defining and accessing entity attributes.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.apply(other) ⇒ Object
Generates Attributes schema for the class.
Instance Method Summary collapse
-
#assign_attributes(attributes) ⇒ Object
Updates the struct’s attributes with the given values.
-
#attributes ⇒ Object
Collects the entity attributes.
-
#attributes=(attributes) ⇒ Object
Replaces the entity’s attributes with the given values.
- #initialize(**properties) ⇒ Object
-
#properties ⇒ Hash<String, Object>
Collects the entity properties.
Class Method Details
.apply(other) ⇒ Object
Generates Attributes schema for the class.
Creates a new Stannum::Schema and sets it as the class’s :Attributes constant. If the superclass is an entity class (and already defines its own Attributes, includes the superclass Attributes in the class Attributes). Finally, includes the class Attributes in the class.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/stannum/entities/attributes.rb', line 71 def apply(other) return unless other.is_a?(Class) return if entity_class?(other) other.const_set(:Attributes, Stannum::Schema.new) if entity_class?(other.superclass) other::Attributes.include(other.superclass::Attributes) end other.include(other::Attributes) end |
Instance Method Details
#assign_attributes(attributes) ⇒ Object
Updates the struct’s attributes with the given values.
This method is used to update some (but not all) of the attributes of the struct. For each key in the hash, it calls the corresponding writer method with the value for that attribute. If the value is nil, this will set the attribute value to the default for that attribute.
Any attributes that are not in the given hash are unchanged, as are any properties that are not attributes.
If the attributes hash includes any keys that do not correspond to an attribute, the struct will raise an error.
125 126 127 128 129 130 131 |
# File 'lib/stannum/entities/attributes.rb', line 125 def assign_attributes(attributes) unless attributes.is_a?(Hash) raise ArgumentError, 'attributes must be a Hash' end set_attributes(attributes, force: false) end |
#attributes ⇒ Object
Collects the entity attributes.
136 137 138 |
# File 'lib/stannum/entities/attributes.rb', line 136 def attributes @attributes.dup end |
#attributes=(attributes) ⇒ Object
Replaces the entity’s attributes with the given values.
This method is used to update all of the attributes of the entity. For each attribute, the writer method is called with the value from the hash, or nil if the corresponding key is not present in the hash. Any nil or missing values set the attribute value to that attribute’s default value, if any. Non-attribute properties are unchanged.
If the attributes hash includes any keys that do not correspond to a valid attribute, the entity will raise an error.
156 157 158 159 160 161 162 |
# File 'lib/stannum/entities/attributes.rb', line 156 def attributes=(attributes) unless attributes.is_a?(Hash) raise ArgumentError, 'attributes must be a Hash' end set_attributes(attributes, force: true) end |
#initialize(**properties) ⇒ Object
101 102 103 104 105 |
# File 'lib/stannum/entities/attributes.rb', line 101 def initialize(**properties) @attributes = {} super end |
#properties ⇒ Hash<String, Object>
Collects the entity properties.
165 166 167 |
# File 'lib/stannum/entities/attributes.rb', line 165 def properties super.merge(attributes) end |