Sha256: fc46fa6b17320eb792eb9293fe90c27a271fd17b70abb66a2f3058da1770a22c

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require 'active_support/core_ext/module/attribute_accessors'
require 'rails_config/options'

require "rails_config/sources/yaml_source"

require 'rails_config/vendor/deep_merge' unless defined?(DeepMerge)

module RailsConfig
  # ensures the setup only gets run once
  @@_ran_once = false

  @@options = nil

  mattr_accessor :const_name
  @@const_name = "Settings"

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

  # 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.
  def self.load_files(*files)
    @@options = Options.new

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

    @@options.load!
  end

  # Loads and sets the settings constant!
  def self.load_and_set_settings(*files)
    Kernel.send(:remove_const, @@const_name) if Kernel.const_defined?(@@const_name)
    load_files(files)
    Kernel.const_set(@@const_name, self)
  end

  def self.reload!
    @@options.reload!
  end

  def self.method_missing(method, *args, &block)
    (@@options[I18n.locale] || @@options[I18n.default_locale] || {}).send(method, *args)
  end
end

# add rails integration
require('rails_config/integration/rails') if defined?(::Rails)

# add sinatra integration
require('rails_config/integration/sinatra') if defined?(::Sinatra)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_config_i18n-0.3.1.3 lib/rails_config/rails_config.rb
rails_config_i18n-0.3.1.2 lib/rails_config/rails_config.rb
rails_config_i18n-0.3.1.1 lib/rails_config/rails_config.rb