Module: Config

Extended by:
Validation::Schema
Defined in:
lib/config.rb,
lib/config/options.rb,
lib/config/version.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/engine.rb,
lib/config/integrations/rails/railtie.rb,
lib/generators/config/install_generator.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'1.4.1'
@@_ran_once =

Ensures the setup only gets run once

false
@@const_name =
'Settings'
@@use_env =
false
@@env_prefix =
@@const_name
@@env_separator =
'.'
@@env_converter =
:downcase
@@env_parse_values =
true
@@knockout_prefix =
nil
@@overwrite_arrays =
true

Class Method Summary collapse

Methods included from Validation::Schema

schema

Class Method Details

.load_and_set_nested_settings(base_path, default_file_name) ⇒ Object



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
# File 'lib/config.rb', line 57

def self.load_and_set_nested_settings(base_path, default_file_name)
  Kernel.send(:remove_const, Config.const_name) if Kernel.const_defined?(Config.const_name)

  config = Options.new
  config.add_source!(File.join(base_path, default_file_name).to_s)
  #config.add_source!(File.join(base_path, priority_file_name).to_s)

  folder_names = Dir.glob("#{base_path}/**/**").select {|f| File.directory? f}

  folder_names.each do |folder_name|
    namespace_path = folder_name.to_s.sub("#{base_path.to_s}/", "")
    namespaces = namespace_path.split("/")

    namespaces.each_with_index do |namespace, i|
      path = namespaces.first(i + 1).join("/")
      namespaced_default_file = File.join(base_path, path, default_file_name).to_s
      #priority_default_file = File.join(base_path, path, priority_file_name).to_s

      config.add_source!(namespaced_default_file.to_s, path)
      #config.add_source!(priority_default_file.to_s, path)
    end
  end

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

  Kernel.const_set(Config.const_name, config)
end

.load_and_set_settings(*files) ⇒ Object

Loads and sets the settings constant!



52
53
54
55
# File 'lib/config.rb', line 52

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 settings file. If a second file is given, then the sections of that file will overwrite existing sections of the first file.



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

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.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/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
      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



98
99
100
# File 'lib/config.rb', line 98

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

.setting_files(config_root, env) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/config.rb', line 86

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



31
32
33
34
# File 'lib/config.rb', line 31

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