Module: Bronze::Entities::Attributes::ClassMethods

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

Overview

Class methods to define when including Attributes in a class.

Instance Method Summary collapse

Instance Method Details

#attribute(attribute_name, attribute_type, attribute_options = {}) ⇒ Attributes::Metadata

Defines an attribute with the specified name and type.

Examples:

Defining an Attribute

class Book
  include Bronze::Entities::Attributes

  attribute :title, String
end # class

book.title
#=> nil

book.title = 'Romance of the Three Kingdoms'
book.title
#=> 'Romance of the Three Kingdoms'

Options Hash (attribute_options):

  • :default (Object, Proc)

    The default value for the attribute. If the attribute value is nil or has not been set, the attribute will be set to the default. If the default is a Proc, the Proc will be called each time and the attribute set to the return value. Otherwise, the attribute will be set to the default value.

  • :default_transform (Boolean)

    If a transform is set, marks the transform as a default transform, which can be overriden when normalizing the attribute.

  • :foreign_key (Boolean)

    Marks the attribute as a foreign key. Will be set to true by association builders, and generally should not be set manually. Defaults to false.

  • :read_only (Boolean)

    If true, the writer method for the attribute will be set as private. Defaults to false.

  • :transform (Class, Bronze::Transform)

    If set, the attribute will be normalized using this transform. By default, certain attribute types will be transformed - BigDecimal, Date, DateTime, Symbol, and Time.

Raises:

  • Builder::Error if the attribute name or attribute type is missing or invalid.



38
39
40
41
42
43
# File 'lib/bronze/entities/attributes.rb', line 38

def attribute(attribute_name, attribute_type, attribute_options = {})
   =
    build_attribute(attribute_name, attribute_type, attribute_options)

  (@attributes ||= {})[.name] = 
end

#attributesHash{Symbol => Attributes::Metadata}

Note:

This method allocates a new hash each time it is called and is not cached. To loop through the attributes, use the ::each_attribute method instead.

Returns the metadata for the attributes defined for the current class.



53
54
55
56
57
58
59
# File 'lib/bronze/entities/attributes.rb', line 53

def attributes
  each_attribute
    .with_object({}) do |(name, ), hsh|
      hsh[name] = 
    end
    .freeze
end

#each_attributeEnumerator #each_attribute {|name, name| ... } ⇒ Object

Overloads:

  • #each_attributeEnumerator

    Returns an enumerator that iterates through the attributes defined on the entity class and any parent classes.

  • #each_attribute {|name, name| ... } ⇒ Object

    Iterates through the attributes defined on the entity class and any parent classes, and yields the name and metadata of each attribute to the block.

    Yield Parameters:



75
76
77
78
79
80
81
82
83
# File 'lib/bronze/entities/attributes.rb', line 75

def each_attribute
  return enum_for(:each_attribute) unless block_given?

  if superclass.respond_to?(:each_attribute)
    superclass.each_attribute { |name, | yield(name, ) }
  end

  (@attributes ||= {}).each { |name, | yield(name, ) }
end