Sha256: 0bce2dd2e8775147f548165514c889ec392a4baf488db2a46caf1c06e91c49a9

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require "mine_setting/version"

require 'yaml'
require 'erb'

# Files:
#   config/settings/base.yml
#   config/settings/secrets.yml
#   config/settings/other_config.yml
#
# File Content:
#   development:
#     foo: bar
#   test:
#     xxx: yyy
#
#
# Code:
#   class MySettings
#     extend MineSetting
#
#     load_dir Rails.root.join('config/settings'), 'development'
#   end
#
# MySettings.base # {foo: bar}
# MySettings.secrets
# MySettings.other_config
#

module MineSetting
  def self.included(cls)
    raise "Cannot include SimpleSettings, please use extend"
  end

  def load_dir(dir_path, env)
    Dir[File.join(dir_path, '*')].each do |filepath|
      filename = File.basename filepath
      next unless filename =~ /^\w+\.ya?ml$/
      puts "Load config '#{filename}'"

      setting_name = filename.gsub(/\.ya?ml/, '')
      eval(%Q{
        def self.#{setting_name}
          @#{setting_name} ||= load_setting('#{filepath}', '#{env}')
        end
      })
    end
  end


  protected

  def load_setting(filepath, env)
    str = File.read(filepath)
    YAML.load(ERB.new(str).result)[env]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mine_setting-0.1.0 lib/mine_setting.rb
mine_setting-0.0.1 lib/mine_setting.rb