Class: Anyway::Env

Inherits:
Object
  • Object
show all
Includes:
Tracing
Defined in:
lib/anyway/env.rb

Overview

Parses environment variables and provides method-like access

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_cast: AutoCast) ⇒ Env

Returns a new instance of Env.



15
16
17
18
19
# File 'lib/anyway/env.rb', line 15

def initialize(type_cast: AutoCast)
  @type_cast = type_cast
  @data = {}
  @traces = {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/anyway/env.rb', line 13

def data
  @data
end

#tracesObject (readonly)

Returns the value of attribute traces.



13
14
15
# File 'lib/anyway/env.rb', line 13

def traces
  @traces
end

#type_castObject (readonly)

Returns the value of attribute type_cast.



13
14
15
# File 'lib/anyway/env.rb', line 13

def type_cast
  @type_cast
end

Instance Method Details

#clearObject



21
22
23
24
# File 'lib/anyway/env.rb', line 21

def clear
  data.clear
  traces.clear
end

#fetch(prefix) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/anyway/env.rb', line 26

def fetch(prefix)
  return data[prefix].deep_dup if data.key?(prefix)

  Tracing.capture do
    data[prefix] = parse_env(prefix)
  end.then do |trace|
    traces[prefix] = trace
  end

  data[prefix].deep_dup
end

#fetch_with_trace(prefix) ⇒ Object



38
39
40
# File 'lib/anyway/env.rb', line 38

def fetch_with_trace(prefix)
  [fetch(prefix), traces[prefix]]
end