Class: Virtus::Attribute::Hash

Inherits:
Virtus::Attribute show all
Defined in:
lib/virtus/attribute/hash.rb

Overview

Handles attributes with Hash type

Defined Under Namespace

Classes: Type

Constant Summary

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary collapse

Attributes inherited from Virtus::Attribute

#coercer, #default_value, #options, #primitive, #type

Class Method Summary collapse

Instance Method Summary collapse

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_typeObject (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_typeObject (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.merge_options!(type, options)
  options[:key_type]   ||= Attribute.build(type.key_type, :strict => options[:strict])
  options[:value_type] ||= Attribute.build(type.value_type, :strict => options[:strict])
end

Instance Method Details

#coerce(input) ⇒ Object

Coerce members

See Also:

  • Virtus::Attribute::Hash.[Attribute[Attribute#coerce]


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

#finalizeObject

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   = options[:key_type].finalize
  @value_type = options[: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.

Returns:



124
125
126
# File 'lib/virtus/attribute/hash.rb', line 124

def finalized?
  super && key_type.finalized? && value_type.finalized?
end