Method: Dotenv#restore

Defined in:
lib/dotenv.rb

#restore(env = @diff&.a, safe: Thread.current == Thread.main) ⇒ Object

Restore ‘ENV` to a given state

Parameters:

  • env (Hash) (defaults to: @diff&.a)

    Hash of keys and values to restore, defaults to the last saved state

  • safe (Boolean) (defaults to: Thread.current == Thread.main)

    Is it safe to modify ‘ENV`? Defaults to `true` in the main thread, otherwise raises an error.

[View source] [View on GitHub]

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dotenv.rb', line 77

def restore(env = @diff&.a, safe: Thread.current == Thread.main)
  # No previously saved or provided state to restore
  return unless env

  diff = Dotenv::Diff.new(b: env)
  return unless diff.any?

  unless safe
    raise ThreadError, "      Dotenv.restore is not thread safe. Use `Dotenv.modify { }` to update ENV for the duration\n      of the block in a thread safe manner, or call `Dotenv.restore(safe: true)` to ignore\n      this error.\n    EOE\n  end\n  instrument(:restore, diff: diff) { ENV.replace(env) }\nend\n".tr("\n", " ")