module Trusty::Environment::ClassMethods

Public Instance Methods

[](key) click to toggle source
# File lib/trusty/environment.rb, line 36
def [](key)
  env[key.to_s.downcase]
end
config(default_value = nil) click to toggle source
# File lib/trusty/environment.rb, line 55
def config(default_value = nil)
  # loads YAML on-the-fly when a key doesn't exist
  @config ||= Hashie::Mash.new do |hash, key|
    hash[key] = methodize_hash load_yaml_file("#{key}.yml", default_value)
  end
end
default_env_section() click to toggle source
# File lib/trusty/environment.rb, line 51
def default_env_section
  @default_env_section ||= ENV['ENV_SECTION'] || ENV['RAILS_ENV'] || ENV['RACK_ENV']
end
env(env_section = default_env_section) click to toggle source

downcases keys to method names

# File lib/trusty/environment.rb, line 41
def env(env_section = default_env_section)
  @env ||= {}
  @env[env_section] ||= methodize_hash env_source(env_section).merge(ENV.to_hash)
end
env_source(env_section = default_env_section) click to toggle source
# File lib/trusty/environment.rb, line 46
def env_source(env_section = default_env_section)
  @env_source ||= {}
  @env_source[env_section] ||= load_yaml_file("env.yml").fetch(env_section, {})
end
load_constants!(options = {}) click to toggle source

load env.yml into constants (e.g. Vars::DATABASE_URL)

# File lib/trusty/environment.rb, line 23
def load_constants!(options = {})
  source = load_yaml_file("env.yml")
  source = source.fetch(options[:env_section] || env_section, {})
  
  source.each do |key, value|
    constant_name = key.to_s.upcase.to_sym
    
    if !constants.include(constant_name) || options[:overwrite] == true
      constant_set constant_name, value.to_s
    end
  end
end
load_env!(options = {}) click to toggle source

load env.yml into ENV (e.g. ENV)

# File lib/trusty/environment.rb, line 11
def load_env!(options = {})
  source = load_yaml_file("env.yml")
  source = source.fetch(options[:env_section] || env_section, {})
  
  source.each do |key, value|
    if !ENV.has_key?(key) || options[:overwrite] == true
      ENV[key.to_s.upcase] = value.to_s
    end
  end
end
method_missing(name, *args, &block) click to toggle source

dynamically add methods that forward to config

# File lib/trusty/environment.rb, line 67
def method_missing(name, *args, &block)
  if !method_name_info(name).special?
    instance_variable_name  = :"@#{name}"
    instance_variable_value = config[name]
    instance_variable_set instance_variable_name, instance_variable_value
    
    define_singleton_method name do
      instance_variable_get instance_variable_name
    end
    
    instance_variable_value
  else
    super
  end
end
paths() click to toggle source
# File lib/trusty/environment.rb, line 62
def paths
  @paths ||= defined?(::Rails) ? [ ::Rails.root.join("config").to_s ] : []
end

Private Instance Methods

load_yaml_file(filename, default_value = {}) click to toggle source
# File lib/trusty/environment.rb, line 85
def load_yaml_file(filename, default_value = {})
  Utilities::Yaml.load_file(filename, paths) || default_value
end
methodize_hash(hash) click to toggle source
# File lib/trusty/environment.rb, line 89
def methodize_hash(hash)
  if hash != nil
    Hashie::Mash.new hash.inject({}){|result, (key, value)| result.merge(key.underscore.downcase => value)}
  end
end