Module: Inquisitive::Environment

Includes:
Inquisitive
Defined in:
lib/inquisitive/environment.rb

Defined Under Namespace

Modules: Parser

Instance Method Summary collapse

Methods included from Inquisitive

[], present?

Instance Method Details

#inquires_about(env_var, opts = {}) ⇒ Object



5
6
7
8
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
# File 'lib/inquisitive/environment.rb', line 5

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, :dynamic).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