Class: Virtus::Attribute::Hash
- Inherits:
-
Object
- Object
- Virtus::Attribute
- Object
- Virtus::Attribute::Hash
- Defined in:
- lib/virtus/attribute/hash.rb
Overview
Hash
Constant Summary
Constants included from TypeLookup
Instance Attribute Summary collapse
-
#key_type ⇒ Virtus::Attribute
readonly
The type to which keys of this hash will be coerced.
-
#value_type ⇒ Virtus::Attribute
readonly
The type to which values of this hash will be coerced.
Attributes inherited from Virtus::Attribute
#coercion_method, #default, #name, #options
Class Method Summary collapse
-
.merge_options(type, options) ⇒ Hash
private
Handles hashes with [key_type => value_type] syntax.
Instance Method Summary collapse
-
#coerce(value) ⇒ Object
private
Coerce a hash with keys and values.
-
#initialize ⇒ Hash
constructor
private
Initializes an instance of Hash.
Methods inherited from Virtus::Attribute
build, #define_accessor_methods, #define_reader_method, #define_writer_method, determine_type, #get, #get!, #inspect, #public_reader?, #public_writer?, #set, #set!, #value_coerced?
Methods included from TypeLookup
#determine_type, extended, #primitive
Methods included from Options
#accept_options, #accepted_options, #options
Constructor Details
#initialize ⇒ Hash
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.
Initializes an instance of Virtus::Attribute::Hash
76 77 78 79 80 81 82 |
# File 'lib/virtus/attribute/hash.rb', line 76 def initialize(*) super @key_type = @options[:key_type] || Object @value_type = @options[:value_type] || Object @key_type_instance = Attribute.build(@name, @key_type) @value_type_instance = Attribute.build(@name, @value_type) end |
Instance Attribute Details
#key_type ⇒ Virtus::Attribute (readonly)
The type to which keys of this hash will be coerced
35 36 37 |
# File 'lib/virtus/attribute/hash.rb', line 35 def key_type @key_type end |
#value_type ⇒ Virtus::Attribute (readonly)
The type to which values of this hash will be coerced
52 53 54 |
# File 'lib/virtus/attribute/hash.rb', line 52 def value_type @value_type end |
Class Method Details
.merge_options(type, options) ⇒ Hash
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.
Handles hashes with [key_type => value_type] syntax.
63 64 65 66 67 68 69 70 71 |
# File 'lib/virtus/attribute/hash.rb', line 63 def self.(type, ) if !type.respond_to?(:size) elsif type.size > 1 raise ArgumentError, "more than one [key => value] pair in `#{type.inspect}`" else .merge(:key_type => type.keys.first, :value_type => type.values.first) end end |
Instance Method Details
#coerce(value) ⇒ 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.
Coerce a hash with keys and values
91 92 93 94 95 96 97 |
# File 'lib/virtus/attribute/hash.rb', line 91 def coerce(value) coerced = super return coerced unless coerced.respond_to?(:each_with_object) coerced.each_with_object({}) do |(key, value), hash| hash[@key_type_instance.coerce(key)] = @value_type_instance.coerce(value) end end |