Class: EnvLint::LintedEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/env_lint/linted_env.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, dot_env_file) ⇒ LintedEnv

Returns a new instance of LintedEnv.



3
4
5
6
# File 'lib/env_lint/linted_env.rb', line 3

def initialize(env, dot_env_file)
  @env = env
  @dot_env_file = dot_env_file
end

Class Method Details

.from_file(file_name) ⇒ Object



25
26
27
# File 'lib/env_lint/linted_env.rb', line 25

def self.from_file(file_name)
  new(ENV, DotEnvFile.from_file(file_name))
end

Instance Method Details

#fetch(name, *args, &block) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/env_lint/linted_env.rb', line 8

def fetch(name, *args, &block)
  name = name.to_s.upcase if name.is_a?(Symbol)
  variable = @dot_env_file.find_variable(name)

  raise(UnknownVariable.new(name)) unless variable

  if variable.optional? && args.empty? && !block_given?
    raise(DefaultValueRequiredForOptionalVariable.new(name))
  end

  block ||= lambda do |i|
    args.empty? ? raise(MissingVariable.new(name)) : args.first
  end

  @env.fetch(name, &block)
end