Module: RailsConfig

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'0.4.2'
@@_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!



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

def self.load_and_set_settings(*files)
  Kernel.send(:remove_const, RailsConfig.const_name) if Kernel.const_defined?(RailsConfig.const_name)
  Kernel.const_set(RailsConfig.const_name, RailsConfig.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 the sections if the first file if they exist in the first file.



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

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 RailsConfig



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_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

    RailsConfig.load_and_set_settings(
      File.join(root.to_s, "config", "settings.yml").to_s,
      File.join(root.to_s, "config", "settings", "#{env}.yml").to_s,
      File.join(root.to_s, "config", "environments", "#{env}.yml").to_s,

      File.join(root.to_s, "config", "settings.local.yml").to_s,
      File.join(root.to_s, "config", "settings", "#{env}.local.yml").to_s,
      File.join(root.to_s, "config", "environments", "#{env}.local.yml").to_s
    )

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

.reload!Object



43
44
45
# File 'lib/rails_config.rb', line 43

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

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

Yields:

  • (_self)

Yield Parameters:

  • _self (RailsConfig)

    the object that the method was called on



17
18
19
20
# File 'lib/rails_config.rb', line 17

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