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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/figs/env.rb', line 25

def method_missing(method, *args, &block)
  if matches_env?(method) then return env.send(method, *args, &block) end
  
  key, punctuation = extract_key_from_method(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



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

def [](key)
  return demarshall(env[key.to_s])
end

#[]=(key, value) ⇒ Object



17
18
19
# File 'lib/figs/env.rb', line 17

def []=(key,value)
  set(key, value)
end

#demarshall(value) ⇒ Object



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

def demarshall(value)
  value.nil? ? nil : YAML::load(value)
end

#envObject



5
6
7
# File 'lib/figs/env.rb', line 5

def env
 @env ||= ::ENV
end

#extract_key_from_method(method) ⇒ Object



41
42
43
# File 'lib/figs/env.rb', line 41

def extract_key_from_method(method)
  method.to_s.upcase.match(/^(.+?)([!?=])?$/).captures
end

#matches_env?(method) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/figs/env.rb', line 60

def matches_env?(method)
  env.respond_to?(method)
end

#missing_key!(key) ⇒ Object

Raises:



45
46
47
# File 'lib/figs/env.rb', line 45

def missing_key!(key)
  raise MissingKey.new("Missing required Figaro configuration key #{key.inspect}.")
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
# File 'lib/figs/env.rb', line 49

def respond_to?(method, *)
  return true if matches_env?(method)
  key, punctuation = extract_key_from_method(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



9
10
11
# File 'lib/figs/env.rb', line 9

def set(key,value)
  env[key.to_s] = value.is_a?(String) ? value : YAML::dump(value)
end