9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/inquisitive/environment.rb', line 9
def inquires_about(env_var, opts={})
env_accessor = opts.fetch(:with, env_var.downcase[/(.*?)(?=(?:_$|$))/])
@__env_accessors__ ||= HashWithIndifferentAccess.new
@__env_accessors__[env_accessor] = env_var
mode = Inquisitive[ opts.fetch(:mode, :static).to_s ]
if mode.dynamic?
define_singleton_method :"#{env_accessor}" do
Inquisitive[Parser[@__env_accessors__[__method__]]]
end
else
@__cached_env__ ||= HashWithIndifferentAccess.new
@__cached_env__[env_accessor] = Inquisitive[Parser[env_var]] if mode.static?
define_singleton_method :"#{env_accessor}" do
if @__cached_env__.has_key? __method__
@__cached_env__[__method__]
else
@__cached_env__[__method__] = Inquisitive[Parser[@__env_accessors__[__method__]]]
end
end
end
present_if = opts.fetch(:present_if, nil)
@__env_presence__ ||= HashWithIndifferentAccess.new
@__env_presence__["#{env_accessor}?"] = present_if if present_if
define_singleton_method :"#{env_accessor}?" do
if @__env_presence__.has_key? __method__
@__env_presence__[__method__] === send(predication(__method__))
else
Inquisitive.present? send(predication(__method__))
end
end
end
|