Class: Carioca::Services::Settings

Inherits:
Hash
  • Object
show all
Defined in:
lib/carioca/services/configuration.rb

Overview

Note:

please do not use Standalone ( dependancy of Configuration class )

settings Hash record utilities class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#deep_symbolize, #method_missing

Constructor Details

#initialize(options = {}) ⇒ Settings

constructor (pre-open the config file in default:YAML)

Options Hash (options):

  • :config_file (String) — default: REQUIRED

    the name of the config file

  • :context (String)

    a context (root) name to bind in YAML Structure

  • :content (String)

    a string (xml or yaml) content for configuration

  • :xml_input (String)

    a boolean if you want load and save in XML



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hash

Instance Attribute Details

#config_fileObject

the name of the config file in YAML format



69
70
71
# File 'lib/carioca/services/configuration.rb', line 69

def config_file
  @config_file
end

Instance Method Details

#save!TrueClass, FalseClass

Note:

TODO save in XML format

save the Hash(self) in the file named by @config_file



101
102
103
104
105
106
107
# 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