Stannum

A library for defining and validating data structures.

Module: Stannum::Entity

Parent Namespace
Stannum
Included Modules
Stannum::Entities::Associations, Stannum::Entities::Attributes, Stannum::Entities::Constraints, Stannum::Entities::PrimaryKey, Stannum::Entities::Properties
Defined In
lib/stannum/entity.rb

Table Of Contents

Overview

Abstract module for defining objects with structured attributes.

Examples

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

Back To Top

Instance Methods

#==(other) =>

Inherited From
Stannum::Entities::Properties

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

#[](property) => Object

Inherited From
Stannum::Entities::Properties

Retrieves the property with the given key.

Parameters

Returns

Raises

#[]=(property, value) => Object

Inherited From
Stannum::Entities::Properties

Sets the given property to the given value.

Parameters

Raises

#assign_associations(associations) => Object

Inherited From
Stannum::Entities::Associations

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.

Parameters

Raises

See Also

#assign_attributes(attributes) => Object

Inherited From
Stannum::Entities::Attributes

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.

Parameters

Raises

See Also

#assign_properties(properties) => Object

Also known as: assign

Inherited From
Stannum::Entities::Properties

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

Raises

See Also

#associations => Hash<String, Object>

Inherited From
Stannum::Entities::Associations

Collects the entity associations.

Returns

#associations=(associations) => Object

Inherited From
Stannum::Entities::Associations

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.

Parameters

Raises

See Also

#attributes => Hash<String, Object>

Inherited From
Stannum::Entities::Attributes

Collects the entity attributes.

Returns

#attributes=(attributes) => Object

Inherited From
Stannum::Entities::Attributes

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.

Parameters

Raises

See Also

#initialize(**properties) => Object

Inherited From
Stannum::Entities::Associations

Parameters

#inspect => String

Inherited From
Stannum::Entities::Properties

Returns

#inspect_with_options(**options) => String

Inherited From
Stannum::Entities::Properties

Parameters

Options Hash (options)

Returns

#primary_key? => Boolean

Inherited From
Stannum::Entities::PrimaryKey

Returns

#primary_key_name => String

Inherited From
Stannum::Entities::PrimaryKey

Returns

Raises

#primary_key_type => Class

Inherited From
Stannum::Entities::PrimaryKey

Returns

Raises

#primary_key_value => Object

Also known as: primary_key

Inherited From
Stannum::Entities::PrimaryKey

Returns

Raises

#properties => Hash<String, Object>

Inherited From
Stannum::Entities::Associations

Collects the entity properties.

Returns

#properties=(properties) => Object

Inherited From
Stannum::Entities::Properties

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

Raises

See Also

#read_association(key, safe: true) => Object

Inherited From
Stannum::Entities::Associations

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.

Parameters

Returns

#read_attribute(key, safe: true) => Object

Inherited From
Stannum::Entities::Attributes

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.

Parameters

Returns

#to_h => Hash<String, Object>

Inherited From
Stannum::Entities::Properties

Returns a Hash representation of the entity.

Returns

See Also

#write_association(key, value, safe: true) => Object

Inherited From
Stannum::Entities::Associations

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.

Parameters

Returns

#write_attribute(key, value, safe: true) => Object

Inherited From
Stannum::Entities::Attributes

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.

Parameters

Returns

Back To Top


Back to Documentation | Versions | 0.4 | Reference | Stannum