module Wms::Config::Mixin

Attributes

config[RW]

Adapted from speakmy.name/2011/05/29/simple-configuration-for-ruby-apps/ config for an instance

Public Class Methods

included(base) click to toggle source

This method is called when someone does 'include Wms::Config::Mixin'

# File lib/wms/config/mixin.rb, line 11
def self.included(base)
  # Add all methods in MixinClassMethod as Class method.
  # When Mixin is included, the user can call:
  # => Mixin.list_mixins
  base.extend(Wms::Config::Mixin::MixinClassMethod)
end

Public Instance Methods

get_config() click to toggle source

return config

# File lib/wms/config/mixin.rb, line 34
def get_config
  @config
end
init_config(params) click to toggle source
# File lib/wms/config/mixin.rb, line 18
def init_config(params)
  @config ||= Hash.new
  params.each do |key, value|
    key = key.to_s unless key.is_a?(Symbol)
    @config[key] = value
  end
end
set_config(name, opts={}) click to toggle source
# File lib/wms/config/mixin.rb, line 26
def set_config(name, opts={})
  @config ||= Hash.new

  name = name.to_s unless name.is_a?(Symbol)
  @config[name] = opts  # ok if this is empty
end
source(filename, options={}) click to toggle source

This is the main point of entry - we call Settings.load! and provide a name of the file to read as it's argument. We can also pass in some options, but at the moment it's being used to allow per-environment overrides in Rails

Example Load without environment

> Settings.load!(“config/appdata/example.yml”)

Load with environment

> Settings.load!(

"#{Rails.root}/config/appdata/env-example.yml",
:env => Rails.env)
# File lib/wms/config/mixin.rb, line 51
def source(filename, options={})
  newsets = YAML::load_file(filename).deep_symbolize_keys
  newsets = newsets[options[:env].to_sym] if \
                                             options[:env] && \
                                             newsets[options[:env].to_sym]
  @default ||= Hash.new
  self.class.deep_merge!(@default, newsets)
end