module Figs::ENV

Public Instance Methods

[](key) click to toggle source
# File lib/figs/env.rb, line 41
def [](key)
  return demarshall(env[key.to_s])
end
[]=(key,value) click to toggle source
# File lib/figs/env.rb, line 45
def []=(key,value)
  set(key, value)
end
demarshall(value) click to toggle source
# File lib/figs/env.rb, line 49
def demarshall(value)
  value.nil? ? nil : YAML::load(value)
end
env() click to toggle source
# File lib/figs/env.rb, line 7
def env
 @env ||= ::ENV
end
extract_key_from_method(method) click to toggle source
# File lib/figs/env.rb, line 69
def extract_key_from_method(method)
  method.to_s.upcase.match(/^(.+?)([!?=])?$/).captures
end
matches_env?(method) click to toggle source
# File lib/figs/env.rb, line 88
def matches_env?(method)
  env.respond_to?(method)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/figs/env.rb, line 53
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
missing_key!(key) click to toggle source
# File lib/figs/env.rb, line 73
def missing_key!(key)
  raise MissingKey.new("Missing required Figaro configuration key #{key.inspect}.")
end
respond_to?(method, *) click to toggle source
Calls superclass method
# File lib/figs/env.rb, line 77
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) click to toggle source

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)
# File lib/figs/env.rb, line 22
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