class Virtus::Attribute::Builder

Builder is used to set up an attribute instance based on input type and options

@private

Attributes

attribute[R]
klass[R]
options[R]
type[R]
type_definition[R]

Public Class Methods

call(type, options = {}) click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 90
def self.call(type, options = {})
  type_definition = TypeDefinition.new(type)

  if type_definition.pending?
    PendingAttribute.new(type, options)
  else
    new(type_definition, options).attribute
  end
end
determine_type(klass, default = nil) click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 101
def self.determine_type(klass, default = nil)
  type = Attribute.determine_type(klass)

  if klass.is_a?(Class)
    type ||=
      if klass < Axiom::Types::Type
        determine_type(klass.primitive)
      elsif EmbeddedValue.handles?(klass)
        EmbeddedValue
      elsif klass < Enumerable && !(klass <= Range)
        Collection
      end
  end

  type || default
end
new(type_definition, options) click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 119
def initialize(type_definition, options)
  @type_definition = type_definition

  initialize_class
  initialize_type
  initialize_options(options)
  initialize_default_value
  initialize_coercer
  initialize_attribute
end

Private Instance Methods

determine_coercer() click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 173
def determine_coercer
  options.fetch(:coercer) { klass.build_coercer(type, options) }
end
determine_visibility() click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 178
def determine_visibility
  default_accessor  = options.fetch(:accessor)
  reader_visibility = options.fetch(:reader, default_accessor)
  writer_visibility = options.fetch(:writer, default_accessor)
  options.update(:reader => reader_visibility, :writer => writer_visibility)
end
initialize_attribute() click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 160
def initialize_attribute
  @attribute = klass.new(type, options)

  @attribute.extend(Accessor)     if options[:name]
  @attribute.extend(Coercible)    if options[:coerce]
  @attribute.extend(NullifyBlank) if options[:nullify_blank]
  @attribute.extend(Strict)       if options[:strict]
  @attribute.extend(LazyDefault)  if options[:lazy]

  @attribute.finalize if options[:finalize]
end
initialize_class() click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 133
def initialize_class
  @klass = self.class.determine_type(type_definition.primitive, Attribute)
end
initialize_coercer() click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 155
def initialize_coercer
  options.update(:coercer => determine_coercer)
end
initialize_default_value() click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 150
def initialize_default_value
  options.update(:default_value => DefaultValue.build(options[:default]))
end
initialize_options(options) click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 143
def initialize_options(options)
  @options = klass.options.merge(:coerce => Virtus.coerce).update(options)
  klass.merge_options!(type, @options)
  determine_visibility
end
initialize_type() click to toggle source

@api private

# File lib/virtus/attribute/builder.rb, line 138
def initialize_type
  @type = klass.build_type(type_definition)
end