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:



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

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
31
# 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
    member_type = member < Virtus::Attribute ? member : Axiom::Types.infer(member)
    klass.new {
      primitive primitive
      member_type member_type
    }
  end
end

.infer_member_type(type) ⇒ Object



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

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:



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

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

Instance Method Details

#coercion_methodObject



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

def coercion_method
  :to_array
end