Class: Cline::OverlayHash

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(*layers) ⇒ OverlayHash

Constructor

Parameters:

  • layers (Array)

    List of Hash-like objects that should be served. In case of conflicting keys, the first ones in the list get priority. Write operations are performed on the first one only.



109
110
111
# File 'lib/cline/overlay_hash.rb', line 109

def initialize(*layers)
  @layers = layers
end

Instance Method Details

#==(other) ⇒ Boolean

Equality check

Parameters:

  • other (Object)

    The other to check equality with

Returns:

  • (Boolean)

    True if objects are equal



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.

Parameters:

  • key (Object)

    The key to look up.

Returns:

  • (Object, nil)

    The value associated with the key, or nil if not found in any layer.



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.

Yields:

  • Optional code called for each key and value.

Yield Parameters:

  • key (Object)

    The key being iterated on.

  • value (Object)

    The value being iterated on.

Returns:

  • (Enumerator)

    The enumerator if no block is given.



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.

Returns:

  • (Boolean)

    true if there are no entries across all layers, false otherwise.



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.

Parameters:

  • key (Object)

    The key to check for.

Returns:

  • (Boolean)

    true if the key is present in at least one layer, false otherwise.



49
50
51
# File 'lib/cline/overlay_hash.rb', line 49

def key?(key)
  @layers.any? { |layer| layer.key?(key) }
end

#keysArray<Object>

Return all unique keys across all layers, in priority order.

Returns:

  • (Array<Object>)

    The list of unique keys.



56
57
58
# File 'lib/cline/overlay_hash.rb', line 56

def keys
  map { |k, _v| k }
end

#sizeInteger

Return the number of unique keys across all layers.

Returns:

  • (Integer)

    The total count of unique keys.



70
71
72
# File 'lib/cline/overlay_hash.rb', line 70

def size
  keys.size
end

#to_hHash

Convert the overlay hash into a plain Ruby Hash.

Returns:

  • (Hash)

    A new hash containing all unique key-value pairs in priority order.



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

#valuesArray<Object>

Return all values across all layers, in priority order.

Returns:

  • (Array<Object>)

    The list of values corresponding to the unique keys.



63
64
65
# File 'lib/cline/overlay_hash.rb', line 63

def values
  map { |_k, v| v }
end