Class: Rdv::Parser

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(data) ⇒ Object



5
6
7
# File 'lib/rdv/parser.rb', line 5

def self.parse data
  (@parser ||= self.new).parse(data)
end

Instance Method Details

#find_blocks(text) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rdv/parser.rb', line 15

def find_blocks text
  text.each_line.reduce([]) do |blocks, line|
    # If line starts with no tab but has content, we start a new block
    if line.match(/^[^\s]+/)
      blocks << Object::Block.new(line)
    # If we have a content line that starts with one or more tabs, we feed
    # last existing block, if we already have one
    elsif line.length > 0 && blocks.length > 0
      blocks.last.add_line(line)
    end

    blocks
  end
end

#parse(data) ⇒ Object



9
10
11
12
13
# File 'lib/rdv/parser.rb', line 9

def parse data
  blocks = find_blocks(data)
  blocks.each(&:build)
  blocks
end