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

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.

Returns:

  • true if the object is a matching entity.



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.

Parameters:

  • property (String, Symbol)

    The property key.

Returns:

  • (Object)

    the value of the property.

Raises:

  • ArgumentError if the key is not a valid property.



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.

Parameters:

  • property (String, Symbol)

    The property key.

  • value (Object)

    The value for the property.

Raises:

  • ArgumentError if the key is not a valid property.



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.

Parameters:

  • properties (Hash)

    The initial properties for the struct.

Raises:

  • ArgumentError if the key is not a valid property.

See Also:



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

Parameters:

  • properties (Hash)

    the properties used to initialize the entity.



12
13
14
# File 'lib/stannum/entities/properties.rb', line 12

def initialize(**properties)
  set_properties(properties, force: true)
end

#inspectString

Returns a string representation of the entity and its properties.

Returns:

  • (String)

    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

#propertiesHash<String, Object>

Collects the entity properties.

Returns:

  • (Hash<String, Object>)

    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.

Parameters:

  • properties (Hash)

    the properties to assign to the entity.

Raises:

  • ArgumentError if any key is not a valid property.

See Also:



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_hHash<String, Object>

Returns a Hash representation of the entity.

Returns:

  • (Hash<String, Object>)

    the entity properties.

See Also:



124
125
126
# File 'lib/stannum/entities/properties.rb', line 124

def to_h
  properties
end