Module: Confset

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"1.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Validation::Schema

schema, schema=

Class Attribute Details

.loggerObject



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

def logger
  @logger ||= Logger.new($stdout).tap do |log|
    log.progname = self.name
  end
end

Class Method Details

.load_and_set_settings(*sources) ⇒ Object

Loads and sets the settings constant!



68
69
70
71
72
# File 'lib/confset.rb', line 68

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

.load_files(*sources) ⇒ 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.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/confset.rb', line 53

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

  # add settings sources
  [sources].flatten.compact.each do |source|
    config.add_source!(source)
  end

  config.add_source!(Sources::EnvSource.new(ENV)) if Confset.use_env

  config.load!
  config
end

.local_setting_files(config_root, env) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/confset.rb', line 83

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 Confset



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/confset/integrations/sinatra.rb', line 11

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

    Confset.load_and_set_settings(Confset.setting_files(File.join(root, "config"), env))

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

.reload!Object



91
92
93
94
95
96
# File 'lib/confset.rb', line 91

def self.reload!
  until Confset.const_name do
    const = Object.const_get(Confset.const_name)
    const.reload!
  end
end

.setting_files(config_root, env) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/confset.rb', line 74

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 (Confset)

    the object that the method was called on



46
47
48
49
# File 'lib/confset.rb', line 46

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