Class: Temple::ImmutableMap
- Includes:
- Enumerable
- Defined in:
- lib/temple/map.rb
Overview
Immutable map class which supports map merging
Direct Known Subclasses
Instance Method Summary collapse
- #[](key) ⇒ Object
- #each ⇒ Object
- #include?(key) ⇒ Boolean
-
#initialize(*map) ⇒ ImmutableMap
constructor
A new instance of ImmutableMap.
- #keys ⇒ Object
- #to_hash ⇒ Object
- #values ⇒ Object
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 |
#each ⇒ Object
21 22 23 |
# File 'lib/temple/map.rb', line 21 def each keys.each {|k| yield(k, self[k]) } end |
#include?(key) ⇒ Boolean
12 13 14 |
# File 'lib/temple/map.rb', line 12 def include?(key) @map.any? {|h| h.include?(key) } end |
#keys ⇒ Object
25 26 27 |
# File 'lib/temple/map.rb', line 25 def keys @map.inject([]) {|keys, h| keys.concat(h.keys) }.uniq end |
#to_hash ⇒ Object
33 34 35 36 37 |
# File 'lib/temple/map.rb', line 33 def to_hash result = {} each {|k, v| result[k] = v } result end |
#values ⇒ Object
29 30 31 |
# File 'lib/temple/map.rb', line 29 def values keys.map {|k| self[k] } end |