Class: Virtus::Attribute::Hash::Type
- Inherits:
-
Struct
- Object
- Struct
- Virtus::Attribute::Hash::Type
- Defined in:
- lib/virtus/attribute/hash.rb
Overview
FIXME: remove this once axiom-types supports it
Instance Attribute Summary collapse
-
#key_type ⇒ Object
Returns the value of attribute key_type.
-
#value_type ⇒ Object
Returns the value of attribute value_type.
Class Method Summary collapse
- .axiom_type?(type) ⇒ Boolean private
- .determine_type(type) ⇒ Object private
- .infer(type) ⇒ Object
- .infer_key_and_value_types(type) ⇒ Object private
- .pending?(primitive) ⇒ Boolean private
Instance Method Summary collapse
- #coercion_method ⇒ Object private
- #primitive ⇒ Object private
Instance Attribute Details
#key_type ⇒ Object
Returns the value of attribute key_type
16 17 18 |
# File 'lib/virtus/attribute/hash.rb', line 16 def key_type @key_type end |
#value_type ⇒ Object
Returns the value of attribute 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.
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 = infer_key_and_value_types(type) key_class = determine_type(.fetch(:key_type, Object)) value_class = determine_type(.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.
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_method ⇒ 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.
78 79 80 |
# File 'lib/virtus/attribute/hash.rb', line 78 def coercion_method :to_hash end |
#primitive ⇒ 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.
83 84 85 |
# File 'lib/virtus/attribute/hash.rb', line 83 def primitive ::Hash end |