A library for defining and validating data structures.
Abstract module for defining objects with structured attributes.
Defining Attributes
class Widget
include Stannum::Entity
attribute :name, String
attribute :description, String, optional: true
attribute :quantity, Integer, default: 0
end
widget = Widget.new(name: 'Self-sealing Stem Bolt')
widget.name #=> 'Self-sealing Stem Bolt'
widget.description #=> nil
widget.quantity #=> 0
widget.attributes #=>
# {
# name: 'Self-sealing Stem Bolt',
# description: nil,
# quantity: 0
# }
Setting Attributes
widget.description = 'A stem bolt, but self sealing.'
widget.attributes #=>
# {
# name: 'Self-sealing Stem Bolt',
# description: 'A stem bolt, but self sealing.',
# quantity: 0
# }
widget.assign_attributes(quantity: 50)
widget.attributes #=>
# {
# name: 'Self-sealing Stem Bolt',
# description: 'A stem bolt, but self sealing.',
# quantity: 50
# }
widget.attributes = (name: 'Inverse Chronoton Emitter')
# {
# name: 'Inverse Chronoton Emitter',
# description: nil,
# quantity: 0
# }
Defining Attribute Constraints
Widget::Contract.matches?(quantity: -5) #=> false
Widget::Contract.matches?(name: 'Capacitor', quantity: -5) #=> true
class Widget
constraint(:quantity) { |qty| qty >= 0 }
end
Widget::Contract.matches?(name: 'Capacitor', quantity: -5) #=> false
Widget::Contract.matches?(name: 'Capacitor', quantity: 10) #=> true
Defining Struct Constraints
Widget::Contract.matches?(name: 'Diode') #=> true
class Widget
constraint { |struct| struct.description&.include?(struct.name) }
end
Widget::Contract.matches?(name: 'Diode') #=> false
Widget::Contract.matches?(
name: 'Diode',
description: 'A low budget Diode',
) #=> true
#==(other) =>
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.
#[](property) => Object
Retrieves the property with the given key.
#[]=(property, value) => Object
Sets the given property to the given value.
#assign_associations(associations) => Object
Updates the struct’s associations with the given values.
This method is used to update some (but not all) of the associations of the struct. For each key in the hash, it calls the corresponding writer method with the value for that association.
Any associations that are not in the given hash are unchanged, as are any properties that are not associations.
If the associations hash includes any keys that do not correspond to an association, the struct will raise an error.
#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.
#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.
#associations => Hash<String, Object>
Collects the entity associations.
#associations=(associations) => Object
Replaces the entity’s associations with the given values.
This method is used to update all of the associations of the entity. For each association, the writer method is called with the value from the hash. Non-association properties are unchanged.
If the associations hash includes any keys that do not correspond to a valid association, the entity will raise an error.
#attributes => Hash<String, Object>
Collects the entity attributes.
#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.
#initialize(**properties) => Object
#inspect => String
#inspect_with_options(**options) => String
#primary_key? => Boolean
#primary_key_name => String
#primary_key_type => Class
#primary_key_value => Object
Also known as:
primary_key
#properties => Hash<String, Object>
Collects the entity properties.
#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.
#read_association(key, safe: true) => Object
Retrieves the association value for the requested key.
If the :safe flag is set, will verify that the association name is valid (a non-empty String or Symbol) and that there is a defined association by that name. By default, :safe is set to true.
#read_attribute(key, safe: true) => Object
Retrieves the attribute value for the requested key.
If the :safe flag is set, will verify that the attribute name is valid (a non-empty String or Symbol) and that there is a defined attribute by that name. By default, :safe is set to true.
#to_h => Hash<String, Object>
Returns a Hash representation of the entity.
#write_association(key, value, safe: true) => Object
Assigns the association value for the requested key.
If the :safe flag is set, will verify that the association name is valid (a non-empty String or Symbol) and that there is a defined association by that name. By default, :safe is set to true.
#write_attribute(key, value, safe: true) => Object
Assigns the attribute value for the requested key.
If the :safe flag is set, will verify that the attribute name is valid (a non-empty String or Symbol) and that there is a defined attribute by that name. By default, :safe is set to true.
Back to Documentation | Versions | 0.4 | Reference | Stannum