Class: AttributedObject::Types::HashOf

Inherits:
AttributedObject::Type show all
Defined in:
lib/attributed_object/types/hash_of.rb

Instance Method Summary collapse

Constructor Details

#initialize(key_type_info, value_type_info) ⇒ HashOf

Returns a new instance of HashOf.



4
5
6
7
# File 'lib/attributed_object/types/hash_of.rb', line 4

def initialize(key_type_info, value_type_info)
  @key_type_info = key_type_info
  @value_type_info = value_type_info
end

Instance Method Details

#coerce(hash) ⇒ Object



16
17
18
19
# File 'lib/attributed_object/types/hash_of.rb', line 16

def coerce(hash)
  raise AttributedObject::UncoercibleValueError.new("Trying to coerce into Hash but value is not an hash") if !hash.is_a?(Hash)
  hash.map { |k,v| [AttributedObjectHelpers::TypeCoerce.coerce(@key_type_info, k), AttributedObjectHelpers::TypeCoerce.coerce(@value_type_info, v)] }
end

#strict_check(hash) ⇒ Object



9
10
11
12
13
14
# File 'lib/attributed_object/types/hash_of.rb', line 9

def strict_check(hash)
  return false if !hash.is_a?(Hash)
  hash.all? do |k,v|
    AttributedObjectHelpers::TypeCheck.check(@key_type_info, k) && AttributedObjectHelpers::TypeCheck.check(@value_type_info, v)
  end
end