Method: Nugrant::Helper::Env::Exporter.export

Defined in:
lib/nugrant/helper/env/exporter.rb

.export(bag, options = {}) ⇒ side-effect

Generic function to export a bag. This walk the bag, for each element, it creates the key using the namer and then forward the key and value to the block if the variable does not override an existing environment variable or if options :override is set to true.

Options:

* :namer => The namer used to transform bag parents into variable name, default to Namer::default().
* :override => If true, variable a exported even when the override an existing env key, default to true.

Parameters:

  • bag

    The bag to export.

Returns:

  • (side-effect)

    Yields each key and value to a block


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/nugrant/helper/env/exporter.rb', line 123

def self.export(bag, options = {})
  namer = options[:namer] || Env::Namer.default()
  override = options.fetch(:override, true)

  variables = {}
  bag.walk do |path, key, value|
    key = namer.call(path)

    variables[key] = value if override or not ENV[key]
  end

  variables.sort().each do |key, value|
    yield key, value
  end
end