class Setting

Constants

NUM_KLASS

Attributes

available_settings[R]

Public Class Methods

[](value) click to toggle source

In [] invocation syntax, we return settings value 'as is' without Hash conversions.

For example, if the YML data is: tax:

default: 0.0
california: 7.5

Then calling Setting returns

{ 'default' => "0.0", 'california' => "7.5"}
# File lib/setting.rb, line 85
def self.[](value)
  self.instance.value_for(value)
end
available_settings() click to toggle source

DEPRECATED: Please use method accessors instead.

# File lib/setting.rb, line 90
def self.available_settings
  self.instance.available_settings
end
load(args = {}) click to toggle source

This method can be called only once.

Parameter hash looks like this:

{  :files => [ "file1.yml", "file2.yml", ...],
   :path  => "/var/www/apps/my-app/current/config/settings",
   :local => true }

If :local => true is set, we will load all *.yml files under :path/local directory after all files in :files have been loaded. “Local” settings thus take precedence by design. See README for more details.

# File lib/setting.rb, line 46
def self.load(args = {})
  raise AlreadyLoaded.new('Settings already loaded') if self.instance.loaded?
  self.instance.load(args)
end
method_missing(method, *args, &block) click to toggle source

In Method invocation syntax we collapse Hash values and return a single value if 'default' is found among keys or Hash has only one key/value pair.

For example, if the YML data is: tax:

default: 0.0
california: 7.5

Then calling Setting.tax returns “0.0”“

This is the preferred method of using settings class.

# File lib/setting.rb, line 68
def self.method_missing(method, *args, &block)
  self.instance.value_for(method, args) do |v, args|
    self.instance.collapse_hashes(v, args)
  end
end
new() click to toggle source
Instance Methods
# File lib/setting.rb, line 98
def initialize
  @available_settings ||= {}
end
reload(args = {}) click to toggle source
# File lib/setting.rb, line 51
def self.reload(args = {})
  self.instance.load(args)
end

Public Instance Methods

collapse_hashes(v, args) click to toggle source

This method performs collapsing of the Hash settings values if the Hash contains 'default' value, or just 1 element.

# File lib/setting.rb, line 132
def collapse_hashes(v, args)
  out = if v.is_a?(Hash)
    if args.empty?
      if v.has_key?("default")
        v['default'].nil? ? "" : v['default']
      elsif v.keys.size == 1
        v.values.first
      else
        v
      end
    else
      v[args.shift.to_s]
    end
  else
    v
  end
  if out.is_a?(Hash) && !args.empty?
      collapse_hashes(out, args)
  elsif out.is_a?(Hash) && out.has_key?('default')
    out['default']
  else
    out
  end
end
has_key?(key) click to toggle source
# File lib/setting.rb, line 102
def has_key?(key)
  @available_settings.has_key?(key) ||
    (key[-1,1] == '?' && @available_settings.has_key?(key.chop))
end
load(params) click to toggle source
# File lib/setting.rb, line 161
def load(params)
  # reset settings hash
  @available_settings = {}
  @loaded = false

  files = []
  path  = params[:path] || Dir.pwd
  params[:files].each do |file|
    files << File.join(path, file)
  end

  if params[:local]
    files << Dir.glob(File.join(path, 'local', '*.yml')).sort
  end

  files.flatten.each do |file|
    begin
      @available_settings.recursive_merge!(YAML::load(ERB.new(IO.read(file)).result) || {}) if File.exists?(file)
    rescue Exception => e
      raise FileError.new("Error parsing file #{file}, with: #{e.message}")
    end
  end

  @loaded = true
  @available_settings
end
loaded?() click to toggle source
# File lib/setting.rb, line 157
def loaded?
  @loaded
end
value_for(key, args = []) { |v, args| ... } click to toggle source
# File lib/setting.rb, line 107
def value_for(key, args = [])
  name = key.to_s
  raise NotFound.new("#{name} was not found") unless has_key?(name)
  bool = false
  if name[-1,1] == '?'
    name.chop!
    bool = true
  end

  v = @available_settings[name]
  if block_given?
    v = yield(v, args)
  end


  if v.is_a?(NUM_KLASS) && bool
    v.to_i > 0
  else
    v
  end
end