Class: Virtus::Attribute::Hash
- Inherits:
-
Virtus::Attribute
- Object
- Virtus::Attribute
- Virtus::Attribute::Hash
- Defined in:
- lib/virtus/attribute/hash.rb
Overview
Handles attributes with Hash type
Defined Under Namespace
Classes: Type
Constant Summary
Constants included from TypeLookup
Instance Attribute Summary collapse
- #key_type ⇒ Object readonly private
- #value_type ⇒ Object readonly private
Attributes inherited from Virtus::Attribute
#coercer, #default_value, #options, #primitive, #type
Class Method Summary collapse
Instance Method Summary collapse
-
#coerce(input) ⇒ Object
Coerce members.
- #finalize ⇒ Object private
- #finalized? ⇒ Boolean private
Methods inherited from Virtus::Attribute
build, build_coercer, coerce, #coercible?, #define_accessor_methods, #initialize, #lazy?, #nullify_blank?, #rename, #required?, #strict?, #value_coerced?
Methods included from TypeLookup
#determine_type, extended, #primitive
Methods included from Options
#accept_options, #accepted_options, #options
Constructor Details
This class inherits a constructor from Virtus::Attribute
Instance Attribute Details
#key_type ⇒ Object (readonly)
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.
11 12 13 |
# File 'lib/virtus/attribute/hash.rb', line 11 def key_type @key_type end |
#value_type ⇒ Object (readonly)
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.
11 12 13 |
# File 'lib/virtus/attribute/hash.rb', line 11 def value_type @value_type end |
Class Method Details
.build_type(definition) ⇒ 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.
89 90 91 |
# File 'lib/virtus/attribute/hash.rb', line 89 def self.build_type(definition) Type.infer(definition.type) end |
.merge_options!(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.
94 95 96 97 |
# File 'lib/virtus/attribute/hash.rb', line 94 def self.(type, ) [:key_type] ||= Attribute.build(type.key_type, :strict => [:strict]) [:value_type] ||= Attribute.build(type.value_type, :strict => [:strict]) end |
Instance Method Details
#coerce(input) ⇒ Object
Coerce members
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/virtus/attribute/hash.rb', line 104 def coerce(input) coerced = super klass = input.kind_of?(::Hash) ? input.class : ::Hash return coerced unless coerced.respond_to?(:each_with_object) coerced.each_with_object(klass.new) do |(key, value), hash| hash[key_type.coerce(key)] = value_type.coerce(value) end end |
#finalize ⇒ 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.
116 117 118 119 120 121 |
# File 'lib/virtus/attribute/hash.rb', line 116 def finalize return self if finalized? @key_type = [:key_type].finalize @value_type = [:value_type].finalize super end |
#finalized? ⇒ 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.
124 125 126 |
# File 'lib/virtus/attribute/hash.rb', line 124 def finalized? super && key_type.finalized? && value_type.finalized? end |