Class: Afterlife::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/afterlife/environment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Environment

Returns a new instance of Environment.



13
14
15
16
# File 'lib/afterlife/environment.rb', line 13

def initialize(repo)
  @repo = repo
  @env = {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/afterlife/environment.rb', line 5

def env
  @env
end

#repoObject (readonly)

Returns the value of attribute repo.



5
6
7
# File 'lib/afterlife/environment.rb', line 5

def repo
  @repo
end

Class Method Details

.from(repo) {|env| ... } ⇒ Object

Yields:



7
8
9
10
11
# File 'lib/afterlife/environment.rb', line 7

def self.from(repo)
  env = new(repo).tap(&:call)
  yield env if block_given?
  env
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/afterlife/environment.rb', line 24

def [](key)
  key = key.gsub('.', '_').upcase
  env_name = "AFTERLIFE_#{key}"
  return ENV[env_name] if ENV.key?(env_name)

  @env[env_name]
end

#callObject



18
19
20
21
22
# File 'lib/afterlife/environment.rb', line 18

def call
  from_flat_envs
  from_branch_envs
  from_stage_envs
end

#set(arg) ⇒ Object

soft set, do not override existent ENVs



33
34
35
36
37
38
39
# File 'lib/afterlife/environment.rb', line 33

def set(arg)
  arg.each do |k, v|
    fail ArgumentError, "key '#{k}' must be a string" unless k.is_a?(String)

    @env[k] ||= ENV.fetch(k, v)
  end
end

#set!(arg) ⇒ Object

forces to be exactly dthe value



42
43
44
45
46
47
48
# File 'lib/afterlife/environment.rb', line 42

def set!(arg)
  arg.each do |k, v|
    fail ArgumentError, "key '#{k}' must be a string" unless k.is_a?(String)

    @env[k] = v
  end
end