Module: Qonf
- Defined in:
- lib/qonf.rb,
lib/qonf/version.rb
Overview
A simple module to get information quickly out of config files
Constant Summary collapse
- VERSION =
"0.0.1"- @@cache =
note, we’re caching the config in memory
{}
- @@environments =
for extraction
Dir.glob("./config/environments/*.rb").map { |filename| File.basename(filename, ".rb") }
Class Method Summary collapse
- .[](item) ⇒ 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
13 14 15 |
# File 'lib/qonf.rb', line 13 def self.[](item) Figaro.env[item.to_s.upcase] || self.get(:qonf, item.to_s.downcase) end |
.get(config, route = []) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/qonf.rb', line 17 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 if !Rails.env.development? && @@cache[config.to_sym] # don't cache in development else base_path = "#{Rails.root}/config/#{config}" formats = { yml: ->(f){ YAML.load(f) }, json: ->(f){ JSON(f.read) } } 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 = cache.deep_symbolize_keys! # Let's do this as symbols cache.deep_merge!(cache.delete(Rails.env.to_sym)) if cache.has_key?(Rails.env.to_sym) # Now merge anything under current env @@environments.each { |env| cache.delete(env.to_sym) } # And remove any other env keys @@cache[config.to_sym] = cache # Store break # And exit end end raise "Unable to find config for #{config} in #{Rails.root}/config/*.{#{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
9 10 11 |
# File 'lib/qonf.rb', line 9 def self.method_missing(method) self.get(:qonf, method) end |
.reset! ⇒ Object
52 53 54 |
# File 'lib/qonf.rb', line 52 def self.reset! @@cache = {} # Clear the cache end |