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

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.

Returns:

  • (Symbol)

    The attribute name as a symbol.



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

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

  attr_name.intern
end

#attributesStannum::Schema

Returns The attributes Schema object for the Entity.

Returns:



41
42
43
# File 'lib/stannum/entities/attributes.rb', line 41

def attributes
  self::Attributes
end