Module: Qonf

Defined in:
lib/qonf.rb,
lib/qonf/railtie.rb,
lib/qonf/version.rb

Overview

A simple module to get information quickly out of config files

Defined Under Namespace

Modules: Config Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.4"
@@cache =

note, we’re caching the config in memory

{}

Class Method Summary collapse

Class Method Details

.[](item) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/qonf.rb', line 38

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



29
30
31
# File 'lib/qonf.rb', line 29

def self.configure(&block)
  Qonf::Config.instance_eval(&block)
end

.get(config, route = []) ⇒ Object



49
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
92
93
# File 'lib/qonf.rb', line 49

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[:json] = ->(f){ JSON(f.read) } if defined?(JSON)
    formats[:yml] = ->(f){ YAML.load(f) } if defined?(YAML)
    formats[:yaml] = ->(f){ YAML.load(f) } if defined?(YAML)
    
    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



34
35
36
# File 'lib/qonf.rb', line 34

def self.method_missing(method)
  self.get(:qonf, method)
end

.reset!Object



95
96
97
# File 'lib/qonf.rb', line 95

def self.reset!
  @@cache = {} # Clear the cache
end