Class: Bronze::Entities::Attributes::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/bronze/entities/attributes/builder.rb

Overview

Service class to define attributes on an entity.

Constant Summary collapse

VALID_OPTIONS =

Provides a list of the valid options for the attribute_options parameter for Builder#build.

%w[
  allow_nil
  default
  default_transform
  foreign_key
  primary_key
  read_only
  transform
].map(&:freeze).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_class) ⇒ Builder

Returns a new instance of Builder.

Parameters:

  • entity_class (Class)

    The entity class on which attributes will be defined.



76
77
78
# File 'lib/bronze/entities/attributes/builder.rb', line 76

def initialize(entity_class)
  @entity_class = entity_class
end

Instance Attribute Details

#entity_classClass (readonly)

Returns the entity class on which attributes will be defined.

Returns:

  • (Class)

    the entity class on which attributes will be defined.



81
82
83
# File 'lib/bronze/entities/attributes/builder.rb', line 81

def entity_class
  @entity_class
end

Class Method Details

.attribute_transform(type, transform) ⇒ Object

Registers a transform as the default transform for attributes with the specified type or a subtype of the specified type.

This default is not retroactive - any attributes already defined will use their existing default transform, if any. If more than one registered transform has a matching type, the most recently defined transform will be used.

Parameters:

  • type (Class)

    The attribute type. When defining an attribute, if the type of the attribute is this class or a subclass of this class and no :transform option is given, the transform for the attribute will be the transform passed to ::attribute_transform.

  • transform (Class, Bronze::Transforms::Transform)

    The transform to use as the default. If this value is a transform instance, the default transform for matching attributes will be the given transform. Otherwise, will set the default transform to the result of ::instance (if defined) or ::new.



44
45
46
# File 'lib/bronze/entities/attributes/builder.rb', line 44

def attribute_transform(type, transform)
  (@attribute_transforms ||= {})[type] = transform
end

Instance Method Details

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

Defines an attribute on the entity class.

Examples:

Defining an Attribute

class Book < Bronze::Entities::Entity; end

book = Book.new
book.title
#=> NoMethodError: undefined method `title'

builder = Bronze::Entities::Attributes::Builder.new(Book)
builder.define_attribute :title, String

book.title
#=> nil

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

Parameters:

  • attribute_name (Symbol, String)

    The name of the attribute to define.

  • attribute_type (Class)

    The type of the attribute to define.

  • attribute_options (Hash) (defaults to: {})

    Additional options for building the attribute.

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.

Returns:

Raises:

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



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bronze/entities/attributes/builder.rb', line 131

def build(attribute_name, attribute_type, attribute_options = {})
  validate_attribute_name(attribute_name)
  validate_attribute_opts(attribute_options)

  characterize(
    attribute_name,
    attribute_type,
    attribute_options
  )
    .tap { || define_property_methods() }
end