Class: Virtus::Attribute::Hash::Type

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

Overview

FIXME: remove this once axiom-types supports it

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#key_typeObject

Returns the value of attribute key_type

Returns:

  • (Object)

    the current value of key_type



16
17
18
# File 'lib/virtus/attribute/hash.rb', line 16

def key_type
  @key_type
end

#value_typeObject

Returns the value of attribute value_type

Returns:

  • (Object)

    the current value of value_type



16
17
18
# File 'lib/virtus/attribute/hash.rb', line 16

def value_type
  @value_type
end

Class Method Details

.axiom_type?(type) ⇒ Boolean

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:



35
36
37
# File 'lib/virtus/attribute/hash.rb', line 35

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

.determine_type(type) ⇒ 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.



40
41
42
43
44
45
46
47
48
# File 'lib/virtus/attribute/hash.rb', line 40

def self.determine_type(type)
  return type if pending?(type)

  if EmbeddedValue.handles?(type)
    type
  else
    Axiom::Types.infer(type)
  end
end

.infer(type) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/virtus/attribute/hash.rb', line 17

def self.infer(type)
  if axiom_type?(type)
    new(type.key_type, type.value_type)
  else
    type_options = infer_key_and_value_types(type)
    key_class    = determine_type(type_options.fetch(:key_type,   Object))
    value_class  = determine_type(type_options.fetch(:value_type, Object))

    new(key_class, value_class)
  end
end

.infer_key_and_value_types(type) ⇒ 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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/virtus/attribute/hash.rb', line 51

def self.infer_key_and_value_types(type)
  return {} unless type.kind_of?(::Hash)

  if type.size > 1
    raise ArgumentError, "more than one [key => value] pair in `#{type}`"
  else
    key_type, value_type = type.keys.first, type.values.first

    key_primitive =
      if key_type.is_a?(Class) && key_type < Attribute && key_type.primitive
        key_type.primitive
      else
        key_type
      end

    value_primitive =
      if value_type.is_a?(Class) && value_type < Attribute && value_type.primitive
        value_type.primitive
      else
        value_type
      end

    { :key_type => key_primitive, :value_type => value_primitive}
  end
end

.pending?(primitive) ⇒ Boolean

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:



30
31
32
# File 'lib/virtus/attribute/hash.rb', line 30

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

Instance Method Details

#coercion_methodObject

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.



78
79
80
# File 'lib/virtus/attribute/hash.rb', line 78

def coercion_method
  :to_hash
end

#primitiveObject

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.



83
84
85
# File 'lib/virtus/attribute/hash.rb', line 83

def primitive
  ::Hash
end