Class: Virtus::Attribute::Hash

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

Overview

Hash

Examples:

class Post
  include Virtus

  attribute :meta, Hash
end

Post.new(:meta => { :tags => %w(foo bar) })

Constant Summary

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary collapse

Attributes inherited from Virtus::Attribute

#coercion_method, #default, #name, #options

Class Method Summary collapse

Instance Method Summary collapse

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

#initializeHash

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_typeVirtus::Attribute (readonly)

The type to which keys of this hash will be coerced

Examples:


class Request
  include Virtus

  attribute :headers, Hash[Symbol => String]
end

Post.attributes[:headers].key_type # => Virtus::Attribute::Symbol

Returns:



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

def key_type
  @key_type
end

#value_typeVirtus::Attribute (readonly)

The type to which values of this hash will be coerced

Examples:


class Request
  include Virtus

  attribute :headers, Hash[Symbol => String]
end

Post.attributes[:headers].value_type # => Virtus::Attribute::String

Returns:



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.

Parameters:

Returns:



63
64
65
66
67
68
69
70
71
# File 'lib/virtus/attribute/hash.rb', line 63

def self.merge_options(type, options)
  if !type.respond_to?(:size)
    options
  elsif type.size > 1
    raise ArgumentError, "more than one [key => value] pair in `#{type.inspect}`"
  else
    options.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

Parameters:

Returns:



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