Class: Anyway::Env
- Inherits:
-
Object
- Object
- Anyway::Env
- Includes:
- Tracing
- Defined in:
- lib/anyway/env.rb,
sig/anyway_config.rbs
Overview
Parses environment variables and provides method-like access
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#env_container ⇒ Object
readonly
Returns the value of attribute env_container.
-
#traces ⇒ Object
readonly
Returns the value of attribute traces.
-
#type_cast ⇒ Object
readonly
Returns the value of attribute type_cast.
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ void
- #fetch(prefix) ⇒ Object
- #fetch_with_trace(prefix) ⇒ [untyped, Tracing::Trace?]
-
#initialize(type_cast: AutoCast, env_container: ENV) ⇒ Env
constructor
A new instance of Env.
Methods included from Tracing
capture, current_trace, current_trace_source, #inspect, source_stack, trace!, trace_stack, with_trace_source
Constructor Details
#initialize(type_cast: AutoCast, env_container: ENV) ⇒ Env
30 31 32 33 34 35 |
# File 'lib/anyway/env.rb', line 30 def initialize(type_cast: AutoCast, env_container: ENV) @type_cast = type_cast @data = {} @traces = {} @env_container = env_container end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
28 29 30 |
# File 'lib/anyway/env.rb', line 28 def data @data end |
#env_container ⇒ Object (readonly)
Returns the value of attribute env_container.
28 29 30 |
# File 'lib/anyway/env.rb', line 28 def env_container @env_container end |
#traces ⇒ Object (readonly)
Returns the value of attribute traces.
28 29 30 |
# File 'lib/anyway/env.rb', line 28 def traces @traces end |
#type_cast ⇒ Object (readonly)
Returns the value of attribute type_cast.
28 29 30 |
# File 'lib/anyway/env.rb', line 28 def type_cast @type_cast end |
Class Method Details
.from_hash(hash, prefix: nil, memo: {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/anyway/env.rb', line 11 def from_hash(hash, prefix: nil, memo: {}) hash.each do |key, value| prefix_with_key = (prefix && !prefix.empty?) ? "#{prefix}_#{key.to_s.upcase}" : key.to_s.upcase if value.is_a?(Hash) from_hash(value, prefix: "#{prefix_with_key}_", memo:) else memo[prefix_with_key] = value.to_s end end memo end |
Instance Method Details
#clear ⇒ void
37 38 39 40 |
# File 'lib/anyway/env.rb', line 37 def clear data.clear traces.clear end |
#fetch(prefix) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/anyway/env.rb', line 42 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) ⇒ [untyped, Tracing::Trace?]
54 55 56 |
# File 'lib/anyway/env.rb', line 54 def fetch_with_trace(prefix) [fetch(prefix), traces[prefix]] end |