Class: Temple::ImmutableMap

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/temple/map.rb

Overview

Immutable map class which supports map merging

Direct Known Subclasses

MutableMap

Instance Method Summary collapse

Constructor Details

#initialize(*map) ⇒ ImmutableMap

Returns a new instance of ImmutableMap.



8
9
10
# File 'lib/temple/map.rb', line 8

def initialize(*map)
  @map = map.compact
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
19
# File 'lib/temple/map.rb', line 16

def [](key)
  @map.each {|h| return h[key] if h.include?(key) }
  nil
end

#eachObject



21
22
23
# File 'lib/temple/map.rb', line 21

def each
  keys.each {|k| yield(k, self[k]) }
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/temple/map.rb', line 12

def include?(key)
  @map.any? {|h| h.include?(key) }
end

#keysObject



25
26
27
# File 'lib/temple/map.rb', line 25

def keys
  @map.inject([]) {|keys, h| keys.concat(h.keys) }.uniq
end

#to_hashObject



33
34
35
36
37
# File 'lib/temple/map.rb', line 33

def to_hash
  result = {}
  each {|k, v| result[k] = v }
  result
end

#valuesObject



29
30
31
# File 'lib/temple/map.rb', line 29

def values
  keys.map {|k| self[k] }
end