class Parameters::Types::Hash
Attributes
key_type[R]
The type to apply to all keys
value_type[R]
The type to apply to all values
Public Class Methods
coerce(value)
click to toggle source
Coerces a value into a Hash
.
@param [::Array, to_hash, ::Object
] value
The value to coerce.
@return [::Hash]
The coerced Hash.
# 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_type()
click to toggle source
The key type of the Hash
type.
@return [Object]
The default key type.
@since 0.4.0
# File lib/parameters/types/hash.rb, line 31 def self.key_type Object end
new(key_type=Object,value_type=Object)
click to toggle source
@param [Type] key_type
@param [Type] value_type
# 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
value_type()
click to toggle source
The value type of the Hash
type.
@return [Object]
The default value type.
@since 0.4.0
# File lib/parameters/types/hash.rb, line 43 def self.value_type Object end
Public Instance Methods
==(other)
click to toggle source
Compares the instance type with another type.
@param [Hash, Type] other
The other type to compare against.
@return [::Boolean]
Specificies whether the instance type has the same key/value types as the other Hash instance type.
@since 0.4.0
Calls superclass method
# File lib/parameters/types/hash.rb, line 93 def ==(other) super(other) && ( (@key_type == other.key_type) && (@value_type == other.value_type) ) end
===(value)
click to toggle source
Determines if the Hash
, and all keys/values, are related to the Type
.
@param [Object, Hash] value
The value to inspect.
@return [Boolean]
Specifies whether the Hash, and all keys/values, match the Type.
# 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)
click to toggle source
Coerces a value into a Hash
, and coerces the keys/values of the Hash
.
@param [::Array, to_hash, ::Object
] value
The value to coerce.
@return [::Hash]
The coerced Hash.
Calls superclass method
# 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