Class: Inventoryfile::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/inventoryfile/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Parser

Returns a new instance of Parser.



3
4
5
# File 'lib/inventoryfile/parser.rb', line 3

def initialize(file)
  @file = file
end

Instance Method Details

#parse(section) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/inventoryfile/parser.rb', line 7

def parse(section)

  items = []

  ignore_case = /(^\s*#)|(^\s*$)/
  find_case = /\s*\[#{section}\]/

  find = false
  f = open(@file)
  f.each do |i|
    line = i.chomp

    case line
    when ignore_case
      next
    when find_case
      find = true
      next
    else
      if line =~ /\s*\[.*\]/
        find = false
      end
      next if find == false

      line.gsub!(/#.*/, "")
      line.strip!
      items << line
    end
  end
  f.close
  items
end