Class: MMETools::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mme_tools/config.rb

Overview

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'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) {|_self| ... } ⇒ Config

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.

Yields:

  • (_self)

Yield Parameters:



45
46
47
48
49
# File 'lib/mme_tools/config.rb', line 45

def initialize(data={},&block)
  @data = {}
  update!(data)
  yield(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object (private)



111
112
113
114
115
116
117
# 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

Class Method Details

.load(filename) ⇒ Object

creates a MMETools::Config object form an already dumped one. filename is the name of the file containing configuration.



94
95
96
97
98
# File 'lib/mme_tools/config.rb', line 94

def self.load(filename)
  obj = self.new
  obj.load filename
  obj
end

Instance Method Details

#[](key) ⇒ Object



75
76
77
# File 'lib/mme_tools/config.rb', line 75

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object



79
80
81
82
83
84
85
# 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

#dump(filename) ⇒ Object Also known as: save

saves configuration into a yaml file named filename



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

#load(filename) ⇒ Object

loads a hash from YAML file filename and merges its contents



88
89
90
# File 'lib/mme_tools/config.rb', line 88

def load(filename)
  update! YAML.load_file(filename)
end

#to_hashObject

creates a hash from a MMETools::Config object.



69
70
71
72
73
# 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

#update!(data) ⇒ Object Also known as: merge!

updates kept configuration with data (Hash or another MMETools::Config object. If a key already exists the corresponding value updates.



53
54
55
56
57
58
59
60
61
62
63
64
# 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