Class: Inventoryfile::Parser
- Inherits:
-
Object
- Object
- Inventoryfile::Parser
- Defined in:
- lib/inventoryfile/parser.rb
Constant Summary collapse
- IGNORE_CASE =
/(^\s*#)|(^\s*$)/- SECTION_CASE =
/\s*\[.*\]/
Instance Method Summary collapse
-
#initialize(filename) ⇒ Parser
constructor
A new instance of Parser.
- #items(section) ⇒ Object
-
#parse(section) ⇒ Object
duplicated.
- #sections ⇒ Object
Constructor Details
#initialize(filename) ⇒ Parser
Returns a new instance of Parser.
7 8 9 10 11 |
# File 'lib/inventoryfile/parser.rb', line 7 def initialize(filename) f = open(filename) @file = f.read.split("\n") f.close end |
Instance Method Details
#items(section) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/inventoryfile/parser.rb', line 18 def items(section) results = [] find_case = /\s*\[#{section}\]/ find = false @file.each do |i| line = i.chomp case line when IGNORE_CASE next when find_case find = true next else if line =~ SECTION_CASE find = false end next if find == false line.gsub!(/#.*/, "") line.strip! results << line end end results end |
#parse(section) ⇒ Object
duplicated
14 15 16 |
# File 'lib/inventoryfile/parser.rb', line 14 def parse(section) items(section) end |
#sections ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/inventoryfile/parser.rb', line 47 def sections results = [] @file.each do |i| line = i.chomp case line when IGNORE_CASE next when SECTION_CASE line.gsub!(/#.*|\[|\]/, "") line.strip! results << line end end results end |