Class: NagiosConfig::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios_config/main.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = {}, objects = []) ⇒ Main

Returns a new instance of Main.



5
6
7
8
# File 'lib/nagios_config/main.rb', line 5

def initialize(variables={}, objects=[])
  self.variables = Hash[*variables.map {|k,v| [k.to_sym,v]}.flatten]
  self.objects = objects
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/nagios_config/main.rb', line 61

def method_missing(name, *args)
  if name.to_s !~ /=$/ && args.empty?
    self[name.to_sym]
  elsif name.to_s =~ /=$/ && args.length == 1
    self[name.to_s.chomp("=").to_sym] = args.first
  else
    super
  end
end

Instance Attribute Details

#objects(of_type = nil) ⇒ Object

Returns the value of attribute objects.



3
4
5
# File 'lib/nagios_config/main.rb', line 3

def objects
  @objects
end

#variablesObject

Returns the value of attribute variables.



3
4
5
# File 'lib/nagios_config/main.rb', line 3

def variables
  @variables
end

Class Method Details

.from_node(node) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nagios_config/main.rb', line 10

def self.from_node(node)
  instance = new
  object_store = instance.objects
  node.nodes.each do |node|
    if node.is_a?(NagiosConfig::Variable)
      name = node.name.value
      value = node.val.value
      case instance[name]
      when nil
        instance[name] = value
      when Array
        instance[name].push(value)
      else
        instance[name] = [instance[name], value]
      end
    elsif node.is_a?(NagiosConfig::Define)
      NagiosConfig::Object.from_node(node, object_store)
    end
  end
  instance
end

.parse(io, include_comments = false) ⇒ Object



32
33
34
# File 'lib/nagios_config/main.rb', line 32

def self.parse(io, include_comments=false)
  from_node(NagiosConfig::Parser.new(!include_comments).parse(io))
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
53
# File 'lib/nagios_config/main.rb', line 50

def ==(other)
  other.is_a?(self.class) && other.variables == variables &&
    other.objects == objects
end

#[](name) ⇒ Object



36
37
38
# File 'lib/nagios_config/main.rb', line 36

def [](name)
  variables[name.to_sym]
end

#[]=(name, value) ⇒ Object



40
41
42
# File 'lib/nagios_config/main.rb', line 40

def []=(name, value)
  variables[name.to_sym] = value
end

#inspectObject



55
56
57
58
59
# File 'lib/nagios_config/main.rb', line 55

def inspect
  "#<#{self.class.name}:#{object_id} @variables=#{variables.inspect}, " <<
  "@objects=#{objects.class.name}:#{objects.object_id}" <<
  "(#{objects.length} items)>"
end