Class: NagiosConfig::Builder

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

Overview

Usage:

conf = NagiosConfig::Builder.new

conf.foo = "bar"
conf.define("test") do |test|
  test.a = "b"
  test.a.comment("foo")
end

puts conf

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = NagiosConfig::Config.new) ⇒ Builder

Returns a new instance of Builder.



16
17
18
# File 'lib/nagios_config/builder.rb', line 16

def initialize(root=NagiosConfig::Config.new)
  self.root = root
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/nagios_config/builder.rb', line 53

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

Instance Attribute Details

#rootObject

Returns the value of attribute root.



14
15
16
# File 'lib/nagios_config/builder.rb', line 14

def root
  @root
end

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  var = get_variable_named(name)
  if var
    extend(var.val.value, var)
  end
end

#[]=(name, value) ⇒ Object



27
28
29
30
# File 'lib/nagios_config/builder.rb', line 27

def []=(name, value)
  set_variable_named(name, value)
  value
end

#breakObject



41
42
43
# File 'lib/nagios_config/builder.rb', line 41

def break
  root.add_node(NagiosConfig::Whitespace.new("\n"))
end

#comment(string) ⇒ Object



45
46
47
# File 'lib/nagios_config/builder.rb', line 45

def comment(string)
  root.add_node(NagiosConfig::Comment.new(string))
end

#define(type) {|self.class.new(define)| ... } ⇒ Object

Yields:



32
33
34
35
36
37
38
39
# File 'lib/nagios_config/builder.rb', line 32

def define(type)
  raise "can't define in a define" if root.is_a?(NagiosConfig::Define)
  define = NagiosConfig::Define.new
  define.add_node(NagiosConfig::Type.new(type.to_s))
  root.add_node(define)
  yield self.class.new(define)
  define
end

#to_sObject



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

def to_s
  NagiosConfig::Formater.new.format(root)
end