Module: Stannum::Entities::Attributes::ClassMethods

Defined in:
lib/stannum/entities/attributes.rb

Overview

Class methods to extend the class when including Attributes.

Instance Method Summary collapse

Instance Method Details

#attribute(attr_name, attr_type, **options) ⇒ Symbol Also known as: define_attribute

Defines an attribute on the entity.

When an attribute is defined, each of the following steps is executed:

  • Adds the attribute to ::Attributes and the .attributes class method.

  • Adds the attribute to #attributes and the associated methods, such as #assign_attributes, #[] and #[]=.

  • Defines reader and writer methods.

Parameters:

  • attr_name (String, Symbol)

    The name of the attribute. Must be a non-empty String or Symbol.

  • attr_type (Class, String)

    The type of the attribute. Must be a Class or Module, or the name of a class or module.

  • options (Hash)

    Additional options for the attribute.

Options Hash (**options):

  • :default (Object)

    The default value for the attribute. Defaults to nil.

  • :primary_key (Boolean)

    true if the attribute represents the primary key for the entity; otherwise false. Defaults to false.

Returns:

  • (Symbol)

    the attribute name as a symbol.



31
32
33
34
35
36
37
38
39
# File 'lib/stannum/entities/attributes.rb', line 31

def attribute(attr_name, attr_type, **options)
  attributes.define(
    name:    attr_name,
    type:    attr_type,
    options:
  )

  attr_name.intern
end

#attributesStannum::Schema

Returns The attributes Schema object for the Entity.

Returns:



43
44
45
# File 'lib/stannum/entities/attributes.rb', line 43

def attributes
  self::Attributes
end