File: env_helper.rb

package info (click to toggle)
ruby-jekyll-github-metadata 2.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: ruby: 2,355; javascript: 107; sh: 41; makefile: 6
file content (31 lines) | stat: -rw-r--r-- 617 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module EnvHelper
  def with_env(*args)
    env_hash = env_args_to_hash(*args)
    old_env = {}
    env_hash.each do |name, value|
      old_env[name] = ENV[name]
      ENV[name] = value
    end
    yield
  ensure
    old_env.each do |name, value|
      ENV[name] = value
    end
  end

  private

  def env_args_to_hash(*args)
    case args.length
    when 2
      env_hash = {}
      env_hash[args.first] = args.last
      return env_hash
    when 1
      return args.first if args.first.is_a? Hash
    end
    raise ArgumentError, "Expect 2 strings or a Hash of VAR => VAL"
  end
end