Class: Virtus::Attribute::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/virtus/attribute/builder.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_definition, options) ⇒ Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Builder.



113
114
115
116
117
118
119
120
121
122
# File 'lib/virtus/attribute/builder.rb', line 113

def initialize(type_definition, options)
  @type_definition = type_definition

  initialize_class
  initialize_type
  initialize_options(options)
  initialize_default_value
  initialize_coercer
  initialize_attribute
end

Instance Attribute Details

#attributeObject (readonly)



81
82
83
# File 'lib/virtus/attribute/builder.rb', line 81

def attribute
  @attribute
end

#klassObject (readonly)



81
82
83
# File 'lib/virtus/attribute/builder.rb', line 81

def klass
  @klass
end

#optionsObject (readonly)



81
82
83
# File 'lib/virtus/attribute/builder.rb', line 81

def options
  @options
end

#typeObject (readonly)



81
82
83
# File 'lib/virtus/attribute/builder.rb', line 81

def type
  @type
end

#type_definitionObject (readonly)



81
82
83
# File 'lib/virtus/attribute/builder.rb', line 81

def type_definition
  @type_definition
end

Class Method Details

.call(type, options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
87
88
89
90
91
92
# File 'lib/virtus/attribute/builder.rb', line 84

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/virtus/attribute/builder.rb', line 95

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