Module: Wms::Config::Mixin

Extended by:
ActiveSupport::Concern
Included in:
Api::Analytic, Input::Base, Widget::Base
Defined in:
lib/wms/config/mixin.rb

Defined Under Namespace

Modules: MixinClassMethod

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

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



8
9
10
# File 'lib/wms/config/mixin.rb', line 8

def config
  @config
end

Class Method Details

.included(base) ⇒ Object

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



11
12
13
14
15
16
# 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

Instance Method Details

#get_configObject

return config



34
35
36
# File 'lib/wms/config/mixin.rb', line 34

def get_config
  @config
end

#init_config(params) ⇒ Object



18
19
20
21
22
23
24
# 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 = {}) ⇒ Object



26
27
28
29
30
31
# 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 = {}) ⇒ Object

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)


51
52
53
54
55
56
57
58
# 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