Sha256: a6f34a70f3bbb3c5a7e32b05b42c9b209d8d636895ddecc727bdef4187006763
Contents?: true
Size: 1.42 KB
Versions: 1
Compression:
Stored size: 1.42 KB
Contents
require "yaml" require "configuration/syntax" require "configuration/version" module Configuration # Returns a hash with configuration for the given class method # called. The configuration is loaded from the file config/+method_called+.yml. # If the hash contains a key in the first level with the same name of environment, # the value of this key is returned. Returns nil if file is not found. # # For example: # ## config/database.yml # # development: # # host: "localhost" # # username: "root" # # ENV["RACK_ENV"] # => "development" # Configuration.database # => { :host => "localhost", :username => "root" } # class << self def method_missing(method, *args) if file = files.select { |f| f =~ /#{method.to_s}/ }[0] hash[method] ||= load_yml(file) end end def files @files ||= Dir.glob("config/*.yml") end def hash @hash ||= {} end def env @env ||= (ENV["RACK_ENV"] || Rails.env).to_sym rescue nil end private def load_yml(file) config = with_symbol_keys YAML.load_file(file) config.is_a?(Hash) && config.has_key?(env) ? config[env] : config end def with_symbol_keys(hash_config) return hash_config unless hash_config.is_a?(Hash) hash_config.inject({}) do |symbolized, (key, value)| symbolized.merge({ key.to_sym => with_symbol_keys(value) }) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
config_yml-0.0.2 | lib/configuration.rb |