Class: Monolens::Type::Map

Inherits:
Object
  • Object
show all
Includes:
ErrorHandling
Defined in:
lib/monolens/type/map.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ErrorHandling

#fail!

Constructor Details

#initialize(key_type, value_type) ⇒ Map

Returns a new instance of Map.



6
7
8
9
# File 'lib/monolens/type/map.rb', line 6

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

Class Method Details

.of(key_type, value_type) ⇒ Object



11
12
13
# File 'lib/monolens/type/map.rb', line 11

def self.of(key_type, value_type)
  Map.new(key_type, value_type)
end

Instance Method Details

#===(instance) ⇒ Object



23
24
25
26
27
# File 'lib/monolens/type/map.rb', line 23

def ===(instance)
  instance.is_a?(::Hash) && instance.all?{|(k,v)|
    @key_type === k && @value_type === v
  }
end

#dress(value, registry, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/monolens/type/map.rb', line 15

def dress(value, registry, &block)
  fail!("Map expected, got #{value.class}", &block) unless value.is_a?(::Hash)

  value.each_with_object({}) do |(k,v), memo|
    memo[@key_type.dress(k, registry, &block)] = @value_type.dress(v, registry, &block)
  end
end