Class: ConfigToolkit::OverrideReader

Inherits:
Reader
  • Object
show all
Defined in:
lib/configtoolkit/overridereader.rb

Overview

This class allows the caller to override configuration paramaters in a given Reader instance with configuration parameters in a second Reader instance.

The override process is similar to a union without duplicate keys. When processing two readers, if a given configuration parameter’s key is present in both readers, the second reader’s parameter replaces the first. The union performed is a “deep” union. Hashes nested in the configuration are unioned as well. Handling arrays is a tad more complicated. If an array specified in the first reader contains no containers (arrays or hashes), and is present in the second reader as well, the second array will completely replace the first. If the array specified in the first reader contains a container, the array will be unioned, and nested containers will be unioned (except for arrays with no containers which are always replaced not unioned.)

See Override.txt for more details.

Additional Note on the Handling of Arrays

The Array handling behavior was chosen from a usage standpoint. Arrays with no container elements are most often considered as one configuration parameter (not a list of configuration parameters). As such, arrays should remain intact during the union (be replaced as opposed to unioned). However, if configurations are nested inside an array, the array is no longer (from a usage standpoint) one configuration parameter, but a container of configuration parameters. Thus, when arrays contain non-container elements, they are unioned instead of simply replaced. If different behavior is required, please feel free to contact us in order to request it.

Defined Under Namespace

Classes: Visitor

Instance Method Summary collapse

Methods inherited from Reader

stream_path

Constructor Details

#initialize(*readers) ⇒ OverrideReader

Description:

This constructs a OverrideReader instance for the list of readers passed in.

Parameters:

readers

A list of objects whose class(es) implements the Reader interface.



48
49
50
51
52
53
54
# File 'lib/configtoolkit/overridereader.rb', line 48

def initialize(*readers)
  if (readers.length == 0)
    raise ArgumentError, "At least one reader must be specified"
  end

  @readers = *readers
end

Instance Method Details

#readObject

Returns:

A hash representing the configuration after all overrides are performed.



179
180
181
182
183
184
185
186
187
# File 'lib/configtoolkit/overridereader.rb', line 179

def read
  aggregate_hash = {}
  
  @readers.each do |reader|
    aggregate_hash = self.class.send(:override, aggregate_hash, reader.read())
  end
  
  aggregate_hash
end