Class: NagiosConfig::Object

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

Constant Summary collapse

@@all =
[]
@@template_cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, variables = {}, objectspace = @@all) ⇒ Object

Returns a new instance of Object.



7
8
9
10
11
# File 'lib/nagios_config/object.rb', line 7

def initialize(type, variables={}, objectspace=@@all)
  self.type = type.to_sym
  self.own_variables = Hash[*variables.map {|k,v| [k.to_sym,v]}.flatten]
  self.objectspace = objectspace
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/nagios_config/object.rb', line 90

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

#objectspaceObject

Returns the value of attribute objectspace.



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

def objectspace
  @objectspace
end

#own_variablesObject

Returns the value of attribute own_variables.



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

def own_variables
  @own_variables
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.clearObject



40
41
42
43
44
# File 'lib/nagios_config/object.rb', line 40

def self.clear
  objectspace.clear
  clear_cache
  nil
end

.clear_cacheObject



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

def self.clear_cache
  @@template_cache.clear
end

.find_template(type, name, objectspace = @@all) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nagios_config/object.rb', line 21

def self.find_template(type, name, objectspace=@@all)
  type = type.to_sym
  name = name.to_s
  key = [type, name, objectspace.object_id]
  cached = @@template_cache[key]
  
  if cached && cached.type == type && cached.name == name
    cached
  else
    @@template_cache[key] = objectspace.find do |obj|
      obj.type == type && obj.name == name
    end
  end
end

.from_node(node, objectspace = @@all) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/nagios_config/object.rb', line 13

def self.from_node(node, objectspace=@@all)
  instance = new(node.type.value, {}, objectspace)
  node.variables.each do |variable|
    instance[variable.name.value] = variable.val.value
  end
  instance
end

.objectspaceObject



46
47
48
# File 'lib/nagios_config/object.rb', line 46

def self.objectspace
  @@all
end

Instance Method Details

#==(other) ⇒ Object



73
74
75
76
# File 'lib/nagios_config/object.rb', line 73

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

#[](name) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/nagios_config/object.rb', line 59

def [](name)
  name = name.to_sym
  result = own_variables[name]
  if result
    result
  elsif own_variables[:use] && name != :name && name != :register
    parent[name]
  end
end

#[]=(name, value) ⇒ Object



69
70
71
# File 'lib/nagios_config/object.rb', line 69

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

#inspectObject



84
85
86
87
88
# File 'lib/nagios_config/object.rb', line 84

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