Class: Bio::FlatFile::Splitter::LineOriented

Inherits:
Template show all
Defined in:
lib/bio/io/flatfile/splitter.rb

Overview

A splitter for line oriented text data.

The given class’s object must have following methods.

Klass#add_header_line(line)
Klass#add_line(line)

where ‘line’ is a string. They normally returns self. If the line is not suitable to add to the current entry, nil or false should be returned. Then, the line is treated as (for add_header_line) the entry data or (for add_line) the next entry’s data.

Instance Attribute Summary

Attributes inherited from Template

#entry, #entry_ended_pos, #entry_pos_flag, #entry_start_pos, #parsed_entry

Instance Method Summary collapse

Constructor Details

#initialize(klass, bstream) ⇒ LineOriented

Creates a new splitter.

klass

database class

bstream

input stream. It must be a BufferedInputStream object.



214
215
216
217
# File 'lib/bio/io/flatfile/splitter.rb', line 214

def initialize(klass, bstream)
  super(klass, bstream)
  self.flag_to_fetch_header = true
end

Instance Method Details

#get_entryObject

get an entry and return the entry as a string



225
226
227
228
229
230
231
# File 'lib/bio/io/flatfile/splitter.rb', line 225

def get_entry
  if e = get_parsed_entry then
    entry
  else
    e
  end
end

#get_parsed_entryObject

get an entry and return the entry as a data class object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/bio/io/flatfile/splitter.rb', line 234

def get_parsed_entry
  p0 = stream_pos()
  ent = @dbclass.new()

  lines = []
  line_overrun = nil

  if flag_to_fetch_header then
    while line = stream.gets("\n")
      unless ent.add_header_line(line) then
        line_overrun = line
        break
      end
      lines.push line
    end
    stream.ungets(line_overrun) if line_overrun
    line_overrun = nil
    self.flag_to_fetch_header = false
  end
      
  while line = stream.gets("\n")
    unless ent.add_line(line) then
      line_overrun = line
      break
    end
    lines.push line
  end
  stream.ungets(line_overrun) if line_overrun
  p1 = stream_pos()

  return nil if lines.empty?

  self.entry_start_pos = p0
  self.entry = lines.join('')
  self.parsed_entry = ent
  self.entry_ended_pos = p1

  return ent
end

#rewindObject

rewinds the stream



275
276
277
278
279
# File 'lib/bio/io/flatfile/splitter.rb', line 275

def rewind
  ret = super
  self.flag_to_fetch_header = true
  ret
end

#skip_leaderObject

do nothing



220
221
222
# File 'lib/bio/io/flatfile/splitter.rb', line 220

def skip_leader
  nil
end