Module: Nuggets::Env::SetMixin

Defined in:
lib/nuggets/env/set_mixin.rb

Instance Method Summary collapse

Instance Method Details

#set(env = {}, clear = true) ⇒ Object Also known as: without

call-seq:

ENV.set([env[, clear]]) => aHash
ENV.set([env[, clear]]) { ... } => anObject

Overrides ENV with env, clearing it beforehand if clear is true. If a block is given, restores ENV to its original state afterwards and returns the result of the block; otherwise returns the original ENV as a hash.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nuggets/env/set_mixin.rb', line 38

def set(env = {}, clear = true)
  old_env = to_hash

  self.clear if clear

  env.each { |key, value|
    key = key.to_s.upcase unless key.is_a?(::String)
    value = value.to_s unless value.is_a?(::String)

    self[key] = value
  }

  block_given? ? yield : old_env
ensure
  set(old_env) if old_env && block_given?
end

#with(env = {}, clear = false) ⇒ Object

call-seq:

ENV.with([env[, clear]]) { ... } => anObject

Temporarily overrides ENV with env for the block execution. See #set.



61
62
63
# File 'lib/nuggets/env/set_mixin.rb', line 61

def with(env = {}, clear = false)
  set(env, clear) { yield }
end