module Bronze::Entities::Attributes::ClassMethods

Class methods to define when including Attributes in a class.

Public Instance Methods

attribute(attribute_name, attribute_type, attribute_options = {}) click to toggle source

Defines an attribute with the specified name and type.

@example 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'

@param (see Attributes::Builder#build)

@option (see Attributes::Builder#build)

@return (see Attributes::Builder#build)

@raise (see Attributes::Builder#build)

# File lib/bronze/entities/attributes.rb, line 38
def attribute(attribute_name, attribute_type, attribute_options = {})
  metadata =
    build_attribute(attribute_name, attribute_type, attribute_options)

  (@attributes ||= {})[metadata.name] = metadata
end
attributes() click to toggle source

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

@return [Hash{Symbol => Attributes::Metadata}] the metadata for each

attribute.

@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.
# File lib/bronze/entities/attributes.rb, line 53
def attributes
  each_attribute
    .with_object({}) do |(name, metadata), hsh|
      hsh[name] = metadata
    end
    .freeze
end
each_attribute() { |name, metadata| ... } click to toggle source

@overload each_attribute

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

@return [Enumerator] the enumerator.

@overload each_attribute

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.

@yieldparam name [Symbol] The name of the attribute.
@yieldparam name [Bronze::Entities::Attributes::Metadata] The metadata
  for the attribute.
# 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, metadata| yield(name, metadata) }
  end

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

Private Instance Methods

attribute_builder() click to toggle source
# File lib/bronze/entities/attributes.rb, line 87
def attribute_builder
  Bronze::Entities::Attributes::Builder.new(self)
end
build_attribute(attribute_name, attribute_type, attribute_options) click to toggle source
# File lib/bronze/entities/attributes.rb, line 91
def build_attribute(attribute_name, attribute_type, attribute_options)
  attribute_builder
    .build(attribute_name, attribute_type, attribute_options)
end