Module: Qonf
- Defined in:
- lib/qonf.rb,
lib/qonf/version.rb
Overview
A simple module to get information quickly out of config files
Defined Under Namespace
Modules: Config
Constant Summary collapse
- VERSION =
"0.0.3"
- @@cache =
note, we’re caching the config in memory
{}
Class Method Summary collapse
- .[](item) ⇒ Object
- .configure(&block) ⇒ Object
- .get(config, route = []) ⇒ Object
-
.method_missing(method) ⇒ Object
Qonf.name -> Qonf.get(:qonf, [:name]) for quick short-hand.
- .reset! ⇒ Object
Class Method Details
.[](item) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/qonf.rb', line 39 def self.[](item) # Use Figaro is defined, otherwise ENV, otherwise try to get from `config/qonf.{json,yml}` if defined?(Figaro) && Figaro.env[item.to_s.upcase] return Figaro.env[item.to_s.upcase] elsif ENV[item.to_s.upcase] ENV[item.to_s.upcase] end self.get(:qonf, item.to_s.downcase) end |
.configure(&block) ⇒ Object
30 31 32 |
# File 'lib/qonf.rb', line 30 def self.configure(&block) Qonf::Config.instance_eval(&block) end |
.get(config, route = []) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/qonf.rb', line 50 def self.get(config, route=[]) route = [route] if route.is_a?(Symbol) || route.is_a?(String) raise "Invalid config" unless config =~ /^\w+$/ config = config.to_sym # Do we just use cached version? if !Qonf::Config.use_cache || !@@cache.has_key?(config.to_sym) base_path = "#{Qonf::Config.base_dir}/#{config}" formats={} formats[:yml] = ->(f){ YAML.load(f) } if defined?(YAML) formats[:json] = ->(f){ JSON(f.read) } if defined?(JSON) raise "Must include JSON or YAML parser" if formats.keys.count == 0 formats.each do |ext,parser| if File.exists?("#{base_path}.#{ext}") cache = parser.call(File.open("#{base_path}.#{ext}")) raise "Invalid Qonf; must be hash" unless cache.is_a?(Hash) cache = symbolize_keys(cache) # Let's do this as symbols # Do we use env key? (e.g. "development") if Qonf::Config.env # TODO: This should be a deep merge? cache.merge!(cache.delete(Qonf::Config.env.to_sym)) if cache.has_key?(Qonf::Config.env.to_sym) # Now merge anything under current env end # Remove any other envs (e.g. "staging") Qonf::Config.environments.each do |environment| cache.delete(environment.to_sym) end @@cache[config.to_sym] = cache # Store break # And exit end end raise "Unable to find config for #{config} in #{base_path}/*.{#{formats.keys.join(',')}}" if @@cache[config.to_sym].nil? end return get_route(@@cache[config.to_sym], route) end |
.method_missing(method) ⇒ Object
Qonf.name -> Qonf.get(:qonf, [:name]) for quick short-hand
35 36 37 |
# File 'lib/qonf.rb', line 35 def self.method_missing(method) self.get(:qonf, method) end |
.reset! ⇒ Object
93 94 95 |
# File 'lib/qonf.rb', line 93 def self.reset! @@cache = {} # Clear the cache end |