Class: HaproxyParser::Sections::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/haproxy_parser/sections/base.rb

Direct Known Subclasses

Backend, Default, Frontend, Global

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/haproxy_parser/sections/base.rb', line 8

def initialize(lines)
  @name = lines[0][1]
  @attr_lines = lines[1..-1]
end

Instance Attribute Details

#attr_linesObject (readonly)

Returns the value of attribute attr_lines.



7
8
9
# File 'lib/haproxy_parser/sections/base.rb', line 7

def attr_lines
  @attr_lines
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/haproxy_parser/sections/base.rb', line 7

def name
  @name
end

Instance Method Details

#attr_value(line) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/haproxy_parser/sections/base.rb', line 33

def attr_value(line)
  case line.size
  when 1
    ""
  when 2
    line[1]
  else
    line[1..-1]
  end
end

#itemsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/haproxy_parser/sections/base.rb', line 13

def items
  @items ||= {}.tap do |data|
    counter = {}
    attr_lines.each do |line|
      if data.key?(line[0])
        if 1 == counter[line[0]]
          data[line[0]] = [] << data[line[0]] << attr_value(line)
        else
          data[line[0]] << attr_value(line)
        end
      else
        data[line[0]] = attr_value(line)
      end
      counter[line[0]] ? counter[line[0]] += 1 : counter[line[0]] = 1
    end
    data[:name] = name
    break data
  end
end