Class: OpenVZ::ConfigHash

Inherits:
Object show all
Includes:
Observable
Defined in:
lib/openvz/confighash.rb

Direct Known Subclasses

OpenVZ::Container::Config, Inventory

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ ConfigHash

Returns a new instance of ConfigHash.



9
10
11
12
# File 'lib/openvz/confighash.rb', line 9

def initialize(data={})
  @data = {}
  update!(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/openvz/confighash.rb', line 37

def method_missing(sym, *args)
  if sym.to_s =~ /(.+)=$/
    self[$1] = args.first
  else
    self[sym]
  end
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/openvz/confighash.rb', line 20

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

#[]=(key, value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/openvz/confighash.rb', line 24

def []=(key, value)
    if @data[key] != value
        if value.class == Hash
          @data[key.to_sym] = Config.new(value)
        else
          @data[key.to_sym] = value
        end
        # Notify observers
        changed
        notify_observers(key, value)
    end
end

#update!(data) ⇒ Object



14
15
16
17
18
# File 'lib/openvz/confighash.rb', line 14

def update!(data)
  data.each do |key, value|
    self[key] = value
  end
end