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.
-
#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.
22 23 24 25 26 |
# File 'lib/stannum/entities/properties.rb', line 22 def ==(other) return false unless other.class == self.class properties == other.properties end |
#[](property) ⇒ Object
Retrieves the property with the given key.
35 36 37 38 39 |
# File 'lib/stannum/entities/properties.rb', line 35 def [](property) tools.assertions.validate_name(property, as: 'property') get_property(property) end |
#[]=(property, value) ⇒ Object
Sets the given property to the given value.
47 48 49 50 51 |
# File 'lib/stannum/entities/properties.rb', line 47 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.
70 71 72 73 74 75 76 |
# File 'lib/stannum/entities/properties.rb', line 70 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
12 13 14 |
# File 'lib/stannum/entities/properties.rb', line 12 def initialize(**properties) set_properties(properties, force: true) end |
#inspect ⇒ String
Returns a string representation of the entity and its properties.
80 81 82 83 84 85 86 |
# File 'lib/stannum/entities/properties.rb', line 80 def inspect mapped = inspectable_properties.reduce('') do |memo, (key, value)| memo + " #{key}: #{value.inspect}" end "#<#{self.class.name}#{mapped}>" end |
#properties ⇒ Hash<String, Object>
Collects the entity properties.
91 92 93 |
# File 'lib/stannum/entities/properties.rb', line 91 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.
111 112 113 114 115 116 117 |
# File 'lib/stannum/entities/properties.rb', line 111 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.
124 125 126 |
# File 'lib/stannum/entities/properties.rb', line 124 def to_h properties end |