class MMETools::Config
Helps keeping configuration parameters, i.e. of any application, grouped within the same object. Also, it can be saved and restored from a YAML file easing edition of configuration very easily. First of all we should create an MMETools::Config
object (see documentation)
config = MMETools::Config.new
Configuration parametres could have any name and can be setted by assignation:
config.my_own_param = {:param => 'my own value'}
Of course, their values can be of any type if they can be marshaled with yaml. To access those parameters we can use the obvious dot attribute way.
puts config.my_own_param # => {:param => 'my own value'}
but also it is possible to chain dots to access inner hashes, for instance:
puts config.my_own_param.param # => 'my_own_value'
Public Class Methods
creates a MMETools::Config
object form an already dumped one. filename
is the name of the file containing configuration.
# File lib/mme_tools/config.rb, line 94 def self.load(filename) obj = self.new obj.load filename obj end
creates a MMETools::Config
object to gracefully keep configuration parameters for any app. if a Hash data
is given it is used to populate it.
cfg = MMETools::Config.new( :param1 => 1, :param2 => 2 )
If a block
is passed, it can be used to setup additional data. self is yielded tot tha block. For instance:
cfg = MMETools::Config.new do |c| c.param1 = 1 c.param2 = 2 end
Of course, both idioms can be combined though block is the last to be evaluated, so its actions may overwrite hash created config data.
# File lib/mme_tools/config.rb, line 45 def initialize(data={},&block) @data = {} update!(data) yield(self) if block_given? end
Public Instance Methods
# File lib/mme_tools/config.rb, line 75 def [](key) @data[key.to_sym] end
# File lib/mme_tools/config.rb, line 79 def []=(key, value) if value.class == Hash @data[key.to_sym] = Config.new(value) else @data[key.to_sym] = value end end
saves configuration into a yaml file named filename
# File lib/mme_tools/config.rb, line 101 def dump(filename) File.open(filename,'w') do |f| YAML.dump(self.to_hash,f) end end
loads a hash from YAML file filename
and merges its contents
# File lib/mme_tools/config.rb, line 88 def load(filename) update! YAML.load_file(filename) end
creates a hash from a MMETools::Config
object.
# File lib/mme_tools/config.rb, line 69 def to_hash @data.inject({}) do |ac,(k,v)| ac.merge! k => ((v.kind_of? self.class) ? v.to_hash : v) end end
updates kept configuration with data
(Hash or another MMETools::Config
object. If a key already exists the corresponding value updates.
# File lib/mme_tools/config.rb, line 53 def update!(data) # can't be used @data.merge because []= is differently defined (below) case data when Hash when MMETools::Config data = data.to_hash else raise ArgumentError, "Only Hash objects or MMETools::Config objects admited" end data.each do |key, value| self[key] = value end end
Private Instance Methods
# File lib/mme_tools/config.rb, line 111 def method_missing(sym, *args) if sym.to_s =~ /(.+)=$/ self[$1] = args.first else self[sym] end end