Class: Virtus::Attribute::Collection::Type

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

Overview

FIXME: temporary hack, remove when Axiom::Type works with EV as member_type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#member_typeObject

Returns the value of attribute member_type

Returns:

  • (Object)

    the current value of member_type



15
16
17
# File 'lib/virtus/attribute/collection.rb', line 15

def member_type
  @member_type
end

#primitiveObject

Returns the value of attribute primitive

Returns:

  • (Object)

    the current value of primitive



15
16
17
# File 'lib/virtus/attribute/collection.rb', line 15

def primitive
  @primitive
end

Class Method Details

.axiom_type?(type) ⇒ Boolean

Returns:



36
37
38
# File 'lib/virtus/attribute/collection.rb', line 36

def self.axiom_type?(type)
  type.is_a?(Class) && type < Axiom::Types::Type
end

.infer(type, primitive) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/virtus/attribute/collection.rb', line 16

def self.infer(type, primitive)
  return type if axiom_type?(type)

  klass  = Axiom::Types.infer(type)
  member = infer_member_type(type) || Object

  if EmbeddedValue.handles?(member) || pending?(member)
    Type.new(primitive, member)
  else
    klass.new {
      primitive primitive
      member_type Axiom::Types.infer(member)
    }
  end
end

.infer_member_type(type) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/virtus/attribute/collection.rb', line 40

def self.infer_member_type(type)
  return unless type.respond_to?(:count)

  member_type =
    if type.count > 1
      raise NotImplementedError, "build SumType from list of types (#{type})"
    else
      type.first
    end

  if member_type.is_a?(Class) && member_type < Attribute && member_type.primitive
    member_type.primitive
  else
    member_type
  end
end

.pending?(primitive) ⇒ Boolean

Returns:



32
33
34
# File 'lib/virtus/attribute/collection.rb', line 32

def self.pending?(primitive)
  primitive.is_a?(String) || primitive.is_a?(Symbol)
end

Instance Method Details

#coercion_methodObject



57
58
59
# File 'lib/virtus/attribute/collection.rb', line 57

def coercion_method
  :to_array
end