Class: Cline::OverlayHash
- Inherits:
-
Object
- Object
- Cline::OverlayHash
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/cline/overlay_hash.rb
Overview
Class merging several Hash-like objects to give a uniform view of them through another Hash-like interface. This is used to provide unifide representations of objects present in both project and global configs. Write operations are performed on the top layer only.
Internal collapse
-
#initialize(*layers) ⇒ OverlayHash
constructor
Constructor.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality check.
-
#[](key) ⇒ Object?
Retrieve the value for a given key from the first layer that contains it.
-
#each {|key, value| ... } ⇒ Enumerator
Loop over all elements.
-
#empty? ⇒ Boolean
Check whether the overlay hash contains any entries.
-
#key?(key) ⇒ Boolean
Check whether the given key exists in any layer.
-
#keys ⇒ Array<Object>
Return all unique keys across all layers, in priority order.
-
#size ⇒ Integer
Return the number of unique keys across all layers.
-
#to_h ⇒ Hash
Convert the overlay hash into a plain Ruby Hash.
-
#values ⇒ Array<Object>
Return all values across all layers, in priority order.
Constructor Details
#initialize(*layers) ⇒ OverlayHash
Constructor
109 110 111 |
# File 'lib/cline/overlay_hash.rb', line 109 def initialize(*layers) @layers = layers end |
Instance Method Details
#==(other) ⇒ Boolean
Equality check
94 95 96 97 |
# File 'lib/cline/overlay_hash.rb', line 94 def ==(other) other.is_a?(OverlayHash) && other.layers == layers end |
#[](key) ⇒ Object?
Retrieve the value for a given key from the first layer that contains it.
38 39 40 41 42 43 |
# File 'lib/cline/overlay_hash.rb', line 38 def [](key) @layers.each do |layer| return layer[key] if layer.key?(key) end nil end |
#each {|key, value| ... } ⇒ Enumerator
Loop over all elements.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cline/overlay_hash.rb', line 20 def each return enum_for(:each) unless block_given? seen = {} @layers.each do |layer| layer.each do |key, value| next if seen.key?(key) seen[key] = true yield key, value end end end |
#empty? ⇒ Boolean
Check whether the overlay hash contains any entries.
77 78 79 |
# File 'lib/cline/overlay_hash.rb', line 77 def empty? none? end |
#key?(key) ⇒ Boolean
Check whether the given key exists in any layer.
49 50 51 |
# File 'lib/cline/overlay_hash.rb', line 49 def key?(key) @layers.any? { |layer| layer.key?(key) } end |
#keys ⇒ Array<Object>
Return all unique keys across all layers, in priority order.
56 57 58 |
# File 'lib/cline/overlay_hash.rb', line 56 def keys map { |k, _v| k } end |
#size ⇒ Integer
Return the number of unique keys across all layers.
70 71 72 |
# File 'lib/cline/overlay_hash.rb', line 70 def size keys.size end |
#to_h ⇒ Hash
Convert the overlay hash into a plain Ruby Hash.
84 85 86 87 88 |
# File 'lib/cline/overlay_hash.rb', line 84 def to_h each_with_object({}) do |(key, value), h| h[key] = value end end |
#values ⇒ Array<Object>
Return all values across all layers, in priority order.
63 64 65 |
# File 'lib/cline/overlay_hash.rb', line 63 def values map { |_k, v| v } end |