Module: Stannum::Entities::Properties
- Included in:
- Stannum::Entity, Struct
- Defined in:
- lib/stannum/entities/properties.rb
Overview
Abstract module for handling heterogenous entity properties.
This module provides a base for accessing and mutating entity properties such as attributes and associations.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares the entity with the other object.
-
#[](property) ⇒ Object
Retrieves the property with the given key.
-
#[]=(property, value) ⇒ Object
Sets the given property to the given value.
-
#assign_properties(properties) ⇒ Object
(also: #assign)
Updates the struct’s properties with the given values.
- #initialize(**properties) ⇒ Object
-
#inspect ⇒ String
A string representation of the entity and its properties.
-
#inspect_with_options(**options) ⇒ String
A string representation of the entity and its properties.
-
#properties ⇒ Hash<String, Object>
Collects the entity properties.
-
#properties=(properties) ⇒ Object
Replaces the entity’s properties with the given values.
-
#to_h ⇒ Hash<String, Object>
Returns a Hash representation of the entity.
Instance Method Details
#==(other) ⇒ Object
Compares the entity with the other object.
The other object must be an instance of the current class. In addition, the properties hashes of the two objects must be equal.
25 26 27 28 29 |
# File 'lib/stannum/entities/properties.rb', line 25 def ==(other) return false unless other.class == self.class properties == other.properties end |
#[](property) ⇒ Object
Retrieves the property with the given key.
38 39 40 41 42 |
# File 'lib/stannum/entities/properties.rb', line 38 def [](property) tools.assertions.validate_name(property, as: 'property') get_property(property) end |
#[]=(property, value) ⇒ Object
Sets the given property to the given value.
50 51 52 53 54 |
# File 'lib/stannum/entities/properties.rb', line 50 def []=(property, value) tools.assertions.validate_name(property, as: 'property') set_property(property, value) end |
#assign_properties(properties) ⇒ Object Also known as: assign
Updates the struct’s properties with the given values.
This method is used to update some (but not all) of the properties of the struct. For each key in the hash, it calls the corresponding writer method with the value for that property. If the value is nil, this will set the property value to the default for that property.
Any properties that are not in the given hash are unchanged.
If the properties hash includes any keys that do not correspond to an property, the struct will raise an error.
73 74 75 76 77 78 79 |
# File 'lib/stannum/entities/properties.rb', line 73 def assign_properties(properties) unless properties.is_a?(Hash) raise ArgumentError, 'properties must be a Hash' end set_properties(properties, force: false) end |
#initialize(**properties) ⇒ Object
15 16 17 |
# File 'lib/stannum/entities/properties.rb', line 15 def initialize(**properties) set_properties(properties, force: true) end |
#inspect ⇒ String
Returns a string representation of the entity and its properties.
83 84 85 |
# File 'lib/stannum/entities/properties.rb', line 83 def inspect end |
#inspect_with_options(**options) ⇒ String
Returns a string representation of the entity and its properties.
95 96 97 98 99 100 |
# File 'lib/stannum/entities/properties.rb', line 95 def (**) address = [:memory_address] ? ":#{memory_address}" : '' mapped = inspect_properties(**) "#<#{self.class.name}#{address}#{mapped}>" end |
#properties ⇒ Hash<String, Object>
Collects the entity properties.
105 106 107 |
# File 'lib/stannum/entities/properties.rb', line 105 def properties {} end |
#properties=(properties) ⇒ Object
Replaces the entity’s properties with the given values.
This method is used to update all of the properties of the entity. For each property, 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 property value to that property’s default value, if any.
If the properties hash includes any keys that do not correspond to a valid property, the entity will raise an error.
125 126 127 128 129 130 131 |
# File 'lib/stannum/entities/properties.rb', line 125 def properties=(properties) unless properties.is_a?(Hash) raise ArgumentError, 'properties must be a Hash' end set_properties(properties, force: true) end |
#to_h ⇒ Hash<String, Object>
Returns a Hash representation of the entity.
138 139 140 |
# File 'lib/stannum/entities/properties.rb', line 138 def to_h properties end |