Module: Figs::ENV
- Extended by:
- ENV
- Included in:
- ENV
- Defined in:
- lib/figs/env.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/figs/env.rb', line 51
def method_missing(method, *args, &block)
if matches_env?(method) then return env.send(method, *args, &block) end
key, punctuation = (method)
_, value = env.detect { |k, _| k.upcase == key }
value = demarshall(value)
case punctuation
when "!" then value || missing_key!(key)
when "?" then !!value
when nil then value
else super
end
end
|
Instance Method Details
#[](key) ⇒ Object
39
40
41
|
# File 'lib/figs/env.rb', line 39
def [](key)
return demarshall(env[key.to_s])
end
|
#[]=(key, value) ⇒ Object
43
44
45
|
# File 'lib/figs/env.rb', line 43
def []=(key,value)
set(key, value)
end
|
#demarshall(value) ⇒ Object
47
48
49
|
# File 'lib/figs/env.rb', line 47
def demarshall(value)
value.nil? ? nil : YAML::load(value)
end
|
#env ⇒ Object
5
6
7
|
# File 'lib/figs/env.rb', line 5
def env
@env ||= ::ENV
end
|
67
68
69
|
# File 'lib/figs/env.rb', line 67
def (method)
method.to_s.upcase.match(/^(.+?)([!?=])?$/).captures
end
|
#matches_env?(method) ⇒ Boolean
86
87
88
|
# File 'lib/figs/env.rb', line 86
def matches_env?(method)
env.respond_to?(method)
end
|
#missing_key!(key) ⇒ Object
71
72
73
|
# File 'lib/figs/env.rb', line 71
def missing_key!(key)
raise MissingKey.new("Missing required Figaro configuration key #{key.inspect}.")
end
|
#respond_to?(method) ⇒ Boolean
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/figs/env.rb', line 75
def respond_to?(method, *)
return true if matches_env?(method)
key, punctuation = (method)
case punctuation
when "!" then env.keys.any? { |k| k.upcase == key } || super
when "?", nil then true
else super
end
end
|
#set(key, value) ⇒ Object
Since Ruby’s implementation of YAML has a set of “basic types” that are implicitly converted from String to the appropriate type, we convert the “basic types” to strings (appropriately) and store them in ENV, so that a Figs application can just call ENV for “basic types” Basic Types handled are
- Integer
- Float
- Null
- Boolean
- Time (in the ISO8601 format)
- Date (in the ISO8601 format)
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/figs/env.rb', line 20
def set(key,value)
env[key.to_s] = begin
case value
when String
value
when Integer, Float
value.to_s
when FalseClass, TrueClass
value.to_s
when Time, Date
value.iso8601
when NilClass
'~'
else
YAML::dump(value)
end
end
end
|