class Carioca::Services::Settings

settings Hash record utilities class @note please do not use Standalone ( dependancy of Configuration class ) @private

Attributes

config_file[RW]

the name of the config file in YAML format

Public Class Methods

new(options = {}) click to toggle source

constructor (pre-open the config file in default:YAML) @param [Hash] options the options records @option options [String] :config_file (REQUIRED) the name of the config file @option options [String] :context a context (root) name to bind in YAML Structure @option options [String] :content a string (xml or yaml) content for configuration @option options [String] :xml_input a boolean if you want load and save in XML

# File lib/carioca/services/configuration.rb, line 77
def initialize(options = {})
  @config_file = options[:config_file]
  @xml_input = options[:xml_input]
  @content  = options[:content]
  @force_array =  options[:force_array]
  newsets = {}
  if @config_file then
    @content = File::readlines(@config_file).join if File::exist?(@config_file)
  end
  if options[:xml_input] then
    newsets = XmlSimple.xml_in( @content, {
                                  'ForceArray' => @force_array,
                                  'KeepRoot' => true,
                                }).deep_symbolize_keys
  else
    newsets = YAML::load(@content).deep_symbolize_keys
  end
  newsets = newsets[options[:context].to_sym] if options[:context] && newsets[options[:context].to_sym]
  deep_merge!(self, newsets)
end

Public Instance Methods

save!() click to toggle source

save the Hash(self) in the file named by @config_file @return [TrueClass,FalseClass] true if save! successfull @note TODO save in XML format

# File lib/carioca/services/configuration.rb, line 101
def save!
  res = false
  File.open(@config_file, "w") do |f|
    res = true if f.write(self.to_yaml)
  end
  return res
end

Private Instance Methods

deep_merge!(target, data) click to toggle source

full recursive merger for hash

# File lib/carioca/services/configuration.rb, line 112
def deep_merge!(target, data)
  merger = proc{|key, v1, v2|
    Settings === v1 && Settings === v2 ? v1.merge(v2, &merger) : v2 }
  target.merge! data, &merger
end