Class: NagiosConfig::Formater

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer = "") ⇒ Formater

Returns a new instance of Formater.



5
6
7
8
9
10
# File 'lib/nagios_config/formater.rb', line 5

def initialize(buffer="")
  self.buffer = buffer
  self.define_indent = 2
  self.define_name_width = 2
  self.define_variable_width = 2
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

#define_indentObject

Returns the value of attribute define_indent.



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

def define_indent
  @define_indent
end

#define_name_widthObject

Returns the value of attribute define_name_width.



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

def define_name_width
  @define_name_width
end

#define_variable_widthObject

Returns the value of attribute define_variable_width.



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

def define_variable_width
  @define_variable_width
end

#in_defineObject

Returns the value of attribute in_define.



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

def in_define
  @in_define
end

Instance Method Details

#format(root) ⇒ Object Also known as: format_Config



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

def format(root)
  root.nodes.each do |node|
    op = "format_#{node.class.name.sub(/^NagiosConfig::/, "").gsub(/::/, "_")}"
    send(op, node)
  end
  buffer
end

#format_Comment(comment) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/nagios_config/formater.rb', line 21

def format_Comment(comment)
  if comment.value.empty?
    buffer << "#\n"
  else
    buffer << comment.value.split(/\n/).map do |comment|
      "##{comment}\n"
    end.join
  end
end

#format_Define(define) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nagios_config/formater.rb', line 48

def format_Define(define)
  buffer << "define "
  name_width = define.variables.map(&:name).map(&:value).map(&:length).max || 0
  value_width = define.variables.map(&:val).map(&:value).map(&:length).max || 0
  variable_width = define_indent + define_name_width + name_width + value_width
  
  self.define_name_width += name_width
  self.define_variable_width += variable_width
  self.in_define = true
  
  format(define)
  
  self.in_define = false
  self.define_variable_width -= variable_width
  self.define_name_width -= name_width
  
  buffer << "}\n"
end

#format_Type(type) ⇒ Object



67
68
69
# File 'lib/nagios_config/formater.rb', line 67

def format_Type(type)
  buffer << "#{type.value} {\n"
end

#format_Variable(variable) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nagios_config/formater.rb', line 35

def format_Variable(variable)
  if in_define
    format = "#{" " * define_indent}%-#{define_name_width}s%s"
  else
    format = "%s=%s"
  end
  var_string = format % [variable.name.value, variable.val.value]
  if variable.trailing_comment
    var_string = "%-#{define_variable_width}s;%s" % [var_string, variable.trailing_comment.value]
  end
  buffer << var_string << "\n"
end

#format_Whitespace(whitespace) ⇒ Object



31
32
33
# File 'lib/nagios_config/formater.rb', line 31

def format_Whitespace(whitespace)
  buffer << whitespace.value
end