Class: Obo::Parser

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

Constant Summary collapse

STANZA_START =
/^\[(.*?)\]/
TAG_VALUE =
/^(.*?):\s*([^!]*)\n/

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Parser

Returns a new instance of Parser.



9
10
11
# File 'lib/obo/parser.rb', line 9

def initialize(filename)
  @io = File.open(filename)
end

Instance Method Details

#elements(io = @io) ⇒ Object



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/obo/parser.rb', line 13

def elements(io = @io)
  Enumerator.new do |yielder|
    header = Header.new
    while io.gets.match TAG_VALUE
      header.add($1, $2)
    end

    yielder << header

    io.gets until $_.match(STANZA_START)
    stanza = Stanza.new($1.strip)

    while io.gets
      case $_
      when TAG_VALUE
        stanza.add($1, $2)
      when STANZA_START
        yielder << stanza
        stanza = Stanza.new($1.strip)
      end
    end

    yielder << stanza
    rewind
  end
end

#rewind(io = @io) ⇒ Object



40
41
42
# File 'lib/obo/parser.rb', line 40

def rewind(io = @io)
  io.pos = 0
end