Module: Config

Defined in:
lib/config.rb,
lib/config/tasks.rb,
lib/config/engine.rb,
lib/config/options.rb,
lib/config/railtie.rb,
lib/config/version.rb,
lib/config/rack/reloader.rb,
lib/config/integration/rails.rb,
lib/config/integration/sinatra.rb,
lib/config/sources/yaml_source.rb,
lib/generators/config/install_generator.rb

Defined Under Namespace

Modules: Generators, Integration, Rack, Sources, Tasks Classes: Engine, Options, Railtie

Constant Summary collapse

VERSION =
'1.0.0'
@@_ran_once =

ensures the setup only gets run once

false
@@const_name =
"Settings"
@@use_env =
false

Class Method Summary collapse

Class Method Details

.load_and_set_settings(*files) ⇒ Object

Loads and sets the settings constant!



39
40
41
42
# File 'lib/config.rb', line 39

def self.load_and_set_settings(*files)
  Kernel.send(:remove_const, Config.const_name) if Kernel.const_defined?(Config.const_name)
  Kernel.const_set(Config.const_name, Config.load_files(files))
end

.load_files(*files) ⇒ Object

Create a populated Options instance from a yaml file. If a second yaml file is given, then the sections of that file will overwrite existing sections of the first file.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/config.rb', line 25

def self.load_files(*files)
  config = Options.new

  # add yaml sources
  [files].flatten.compact.uniq.each do |file|
    config.add_source!(file.to_s)
  end

  config.load!
  config.load_env! if @@use_env
  config
end

.registered(app) ⇒ Object

provide helper to register within your Sinatra app

set :root, File.dirname(__FILE__) register Config



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/config/integration/sinatra.rb', line 9

def self.registered(app)
  app.configure do |inner_app|

    env = inner_app.environment || ENV["RACK_ENV"]
    root = inner_app.root

    # use Padrino settings if applicable
    if defined?(Padrino)
      env = Padrino.env
      root = Padrino.root
    end

    Config.load_and_set_settings(Config.setting_files(File.join(root, 'config'), env))

    inner_app.use(::Config::Rack::Reloader) if inner_app.development?
  end
end

.reload!Object



56
57
58
# File 'lib/config.rb', line 56

def self.reload!
  Kernel.const_get(Config.const_name).reload!
end

.setting_files(config_root, env) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/config.rb', line 44

def self.setting_files(config_root, env)
  [
    File.join(config_root, "settings.yml").to_s,
    File.join(config_root, "settings", "#{env}.yml").to_s,
    File.join(config_root, "environments", "#{env}.yml").to_s,

    File.join(config_root, "settings.local.yml").to_s,
    File.join(config_root, "settings", "#{env}.local.yml").to_s,
    File.join(config_root, "environments", "#{env}.local.yml").to_s
  ].freeze
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Config)

    the object that the method was called on



18
19
20
21
# File 'lib/config.rb', line 18

def self.setup
  yield self if @@_ran_once == false
  @@_ran_once = true
end