class Globals

Public Class Methods

load(content, env, variables={}) click to toggle source
# File lib/globals.rb, line 25
def self.load(content, env, variables={})
  erb = ERB.new(content).result
  yaml = YAML.load(erb % variables)
  hash = yaml['defaults'] || {}
  hash.recursive_merge!(yaml[env] || {})

  unless hash["feature"].nil?
    hash["feature"].each_pair do |k, v|
      raise "A feature can only be true or false." if ![true, false].include?(v)
    end
  end

  hash
end
new(hash, env) click to toggle source
# File lib/globals.rb, line 17
def initialize(hash, env)
  @globals = hash
  @environment = env
  @cache = {}

  define_accessors
end
read(content, env='development', variables={}) click to toggle source
# File lib/globals.rb, line 40
def self.read(content, env='development', variables={})
  env = env.to_s
  hash = load(content, env, variables)
  new(hash, env)
end

Public Instance Methods

method_missing(field) click to toggle source
# File lib/globals.rb, line 46
def method_missing(field)
  nil
end
override(override_content) click to toggle source
# File lib/globals.rb, line 50
def override(override_content)
  overrides = self.class.load(override_content, @environment)

  if overrides
    @globals.recursive_merge! overrides
    @cache.clear
    define_accessors
  end

  self
end
to_hash() click to toggle source
# File lib/globals.rb, line 62
def to_hash
  @globals
end

Private Instance Methods

define_accessors() click to toggle source
# File lib/globals.rb, line 68
def define_accessors
  @globals.each_pair do |key, value|
    define_singleton_method key do
      if value.is_a?(Hash)
        # Cache the instances for efficiency and allowing stubbing.
        @cache[key] ||= Globals.new(value, @environment)
      else
        value
      end
    end
  end
end