Module: Config

Extended by:
Validation::Schema
Defined in:
lib/config.rb,
lib/config/options.rb,
lib/config/version.rb,
lib/config/configuration.rb,
lib/config/rack/reloader.rb,
lib/config/validation/error.rb,
lib/config/validation/schema.rb,
lib/config/integrations/heroku.rb,
lib/config/sources/hash_source.rb,
lib/config/sources/yaml_source.rb,
lib/config/validation/validate.rb,
lib/config/integrations/sinatra.rb,
lib/config/integrations/rails/railtie.rb,
lib/generators/config/install_generator.rb

Defined Under Namespace

Modules: Generators, Integrations, Rack, Sources, Validation Classes: Configuration, Options

Constant Summary collapse

VERSION =
'2.2.3'.freeze

Class Method Summary collapse

Methods included from Validation::Schema

schema, schema=

Class Method Details

.load_and_set_settings(*files) ⇒ Object

Loads and sets the settings constant!



49
50
51
52
53
# File 'lib/config.rb', line 49

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

.load_files(*files) ⇒ Object

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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/config.rb', line 36

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

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

  config.load!
  config
end

.local_setting_files(config_root, env) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/config.rb', line 64

def self.local_setting_files(config_root, env)
  [
    (File.join(config_root, 'settings.local.yml').to_s if env != 'test'),
    File.join(config_root, 'settings', "#{env}.local.yml").to_s,
    File.join(config_root, 'environments', "#{env}.local.yml").to_s
  ].compact
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/integrations/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 if Padrino.respond_to?(:env)
      root = Padrino.root if Padrino.respond_to?(: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



72
73
74
# File 'lib/config.rb', line 72

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

.setting_files(config_root, env) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/config.rb', line 55

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,
    *local_setting_files(config_root, env)
  ].freeze
end

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Config)

    the object that the method was called on



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

def self.setup
  yield self unless @_ran_once
  @_ran_once = true
end