Sha256: b3822bad3d2ad95fb6ba01721aa6bc9576c8d89998b002dc309dc5574c30e823

Contents?: true

Size: 1.02 KB

Versions: 11

Compression:

Stored size: 1.02 KB

Contents

require 'erb'
require 'yaml'

require 'rest-graph'
require 'rest-graph/rails_util' if Object.const_defined?(:Rails)

module RestGraph::LoadConfig
  module_function
  def auto_load!
    RestGraph::LoadConfig.load_if_rails!
  end

  def load_if_rails!
    return unless Object.const_defined?(:Rails)
    root = Rails.root
    file = ["#{root}/config/rest-graph.yaml", # YAML should use .yaml
            "#{root}/config/rest-graph.yml"].find{|path| File.exist?(path)}
    return unless file

    RestGraph::LoadConfig.load_config!(file, Rails.env)
  end

  def load_config! file, env
    config   = YAML.load(ERB.new(File.read(file)).result(binding))
    defaults = config[env]
    return unless defaults

    mod = Module.new
    mod.module_eval(defaults.inject([]){ |r, (k, v)|
      r << <<-RUBY
             def default_#{k}
               # quote strings, leave others free (e.g. false, numbers, etc)
               #{v.kind_of?(String) ? "'#{v}'" : v}
             end
             RUBY
    }.join)

    RestGraph.send(:extend, mod)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rest-graph-1.6.0 lib/rest-graph/load_config.rb
rest-graph-1.5.0 lib/rest-graph/load_config.rb
rest-graph-1.4.6 lib/rest-graph/load_config.rb
rest-graph-1.4.5 lib/rest-graph/load_config.rb
rest-graph-1.4.4 lib/rest-graph/load_config.rb
rest-graph-1.4.3 lib/rest-graph/load_config.rb
rest-graph-1.4.2 lib/rest-graph/load_config.rb
rest-graph-1.4.1 lib/rest-graph/load_config.rb
rest-graph-1.4.0 lib/rest-graph/load_config.rb
rest-graph-1.3.0 lib/rest-graph/load_config.rb
rest-graph-1.2.1 lib/rest-graph/load_config.rb