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.


119
120
121
122
123
124
125
126
127
128
# 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

Instance Attribute Details

#attributeObject (readonly)


87
88
89
# File 'lib/virtus/attribute/builder.rb', line 87

def attribute
  @attribute
end

#klassObject (readonly)


87
88
89
# File 'lib/virtus/attribute/builder.rb', line 87

def klass
  @klass
end

#optionsObject (readonly)


87
88
89
# File 'lib/virtus/attribute/builder.rb', line 87

def options
  @options
end

#typeObject (readonly)


87
88
89
# File 'lib/virtus/attribute/builder.rb', line 87

def type
  @type
end

#type_definitionObject (readonly)


87
88
89
# File 'lib/virtus/attribute/builder.rb', line 87

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.


90
91
92
93
94
95
96
97
98
# 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) ⇒ 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.


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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