Class: Parameters::Types::Hash

Inherits:
Object show all
Defined in:
lib/parameters/types/hash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

===, to_ruby

Methods inherited from Type

#<, #<=, ===, to_ruby

Constructor Details

#initialize(key_type = Object, value_type = Object) ⇒ Hash

Returns a new instance of Hash.

Parameters:

  • key_type (Type) (defaults to: Object)
  • value_type (Type) (defaults to: Object)


18
19
20
21
# File 'lib/parameters/types/hash.rb', line 18

def initialize(key_type=Object,value_type=Object)
  @key_type   = key_type
  @value_type = value_type
end

Instance Attribute Details

#key_typeObject (readonly)

The type to apply to all keys



8
9
10
# File 'lib/parameters/types/hash.rb', line 8

def key_type
  @key_type
end

#value_typeObject (readonly)

The type to apply to all values



11
12
13
# File 'lib/parameters/types/hash.rb', line 11

def value_type
  @value_type
end

Class Method Details

.coerce(value) ⇒ ::Hash

Coerces a value into a Hash.

Parameters:

  • value (::Array, #to_hash, ::Object)

    The value to coerce.

Returns:

  • (::Hash)

    The coerced Hash.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/parameters/types/hash.rb', line 56

def self.coerce(value)
  case value
  when ::Hash
    value
  when ::Array
    ::Hash[*value]
  else
    if value.respond_to?(:to_hash)
      value.to_hash
    else
      raise(TypeError,"cannot coerce #{value.inspect} into a Hash")
    end
  end
end

.key_typeObject

The key type of the Hash type.

Returns:

  • (Object)

    The default key type.

Since:

  • 0.4.0



31
32
33
# File 'lib/parameters/types/hash.rb', line 31

def self.key_type
  Object
end

.value_typeObject

The value type of the Hash type.

Returns:

  • (Object)

    The default value type.

Since:

  • 0.4.0



43
44
45
# File 'lib/parameters/types/hash.rb', line 43

def self.value_type
  Object
end

Instance Method Details

#==(other) ⇒ ::Boolean

Compares the instance type with another type.

Parameters:

  • other (Hash, Type)

    The other type to compare against.

Returns:

  • (::Boolean)

    Specificies whether the instance type has the same key/value types as the other Hash instance type.

Since:

  • 0.4.0



93
94
95
96
97
98
# File 'lib/parameters/types/hash.rb', line 93

def ==(other)
  super(other) && (
    (@key_type   == other.key_type) &&
    (@value_type == other.value_type)
  )
end

#===(value) ⇒ Boolean

Determines if the Hash, and all keys/values, are related to the Type.

Parameters:

Returns:

  • (Boolean)

    Specifies whether the Hash, and all keys/values, match the Type.



109
110
111
112
113
114
# File 'lib/parameters/types/hash.rb', line 109

def ===(value)
  (self.class === value) && value.entries.all? do |k,v|
    (@key_type.nil? || @key_type === k) &&
    (@value_type.nil? || @value_type === v)
  end
end

#coerce(value) ⇒ ::Hash

Coerces a value into a Hash, and coerces the keys/values of the Hash.

Parameters:

  • value (::Array, #to_hash, ::Object)

    The value to coerce.

Returns:

  • (::Hash)

    The coerced Hash.



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/parameters/types/hash.rb', line 125

def coerce(value)
  hash         = super(value)
  coerced_hash = {}

  hash.each do |k,v|
    k = @key_type.coerce(k)   if @key_type
    v = @value_type.coerce(v) if @value_type

    coerced_hash[k] = v
  end

  return coerced_hash
end

#to_rubyHash{Class => Class}

The Ruby Type that the Hash Type instance represents.

Returns:

  • (Hash{Class => Class})

    A singleton Hash containing the key and value types.



77
78
79
# File 'lib/parameters/types/hash.rb', line 77

def to_ruby
  ::Hash[@key_type.to_ruby => @value_type.to_ruby]
end