Class: NagiosConfig::Node

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Node

Returns a new instance of Node.



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

def initialize(value=nil)
  self.value = value
  self.nodes = []
end

Instance Attribute Details

#nodesObject

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.allow(*node_types) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/nagios_config/node.rb', line 10

def self.allow(*node_types)
  if node_types.any?
    allow.push(*node_types)
  else
    @allowed_node_types ||= []
  end
end

.node(name, klass = generate_node_type(name)) {|klass| ... } ⇒ Object

Yields:

  • (klass)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nagios_config/node.rb', line 27

def self.node(name, klass=generate_node_type(name))
  define_method(name) do
    nodes.find {|n| n.is_a?(klass)}
  end
  define_method("#{name}=") do |value|
    remove_node(name)
    add_node(value) if value
  end
  allow(klass)
  yield klass if block_given?
end

.nodes(name, klass, singular = name.to_s.chomp("s")) {|klass| ... } ⇒ Object

Yields:

  • (klass)


18
19
20
21
22
23
24
25
# File 'lib/nagios_config/node.rb', line 18

def self.nodes(name, klass, singular=name.to_s.chomp("s"))
  define_method(name) do
    nodes.select {|n| n.is_a?(klass)}
  end
  alias_method :"add_#{singular}", :add_node
  allow(klass)
  yield klass if block_given?
end

Instance Method Details

#add_node(node) ⇒ Object



43
44
45
46
47
# File 'lib/nagios_config/node.rb', line 43

def add_node(node)
  raise "node type #{node.class} not allowed in #{self.class}" unless allow?(node)
  nodes << node
  self
end

#after(node) ⇒ Object



54
55
56
# File 'lib/nagios_config/node.rb', line 54

def after(node)
  nodes[nodes.index(node) + 1]
end

#allow?(node) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/nagios_config/node.rb', line 39

def allow?(node)
  self.class.allow.include?(node.class)
end

#before(node) ⇒ Object



58
59
60
61
62
63
# File 'lib/nagios_config/node.rb', line 58

def before(node)
  index = nodes.index(node) - 1
  if index >= 0
    nodes[index]
  end
end

#insert_after(position_node, node) ⇒ Object



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

def insert_after(position_node, node)
  nodes.insert(nodes.index(position_node) + 1, node)
end

#insert_before(position_node, node) ⇒ Object



65
66
67
# File 'lib/nagios_config/node.rb', line 65

def insert_before(position_node, node)
  nodes.insert(nodes.index(position_node), node)
end

#remove_node(node) ⇒ Object



49
50
51
52
# File 'lib/nagios_config/node.rb', line 49

def remove_node(node)
  nodes.delete(node)
  self
end