Class: Mspire::Mzml::IOIndex

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mspire/mzml/io_index.rb

Overview

an index that retrieves its objects on the fly by index from the IO object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#index_by, #uniq_by

Constructor Details

#initialize(io, byte_index, link) ⇒ IOIndex

byte_index will typically be an Mspire::Mzml::Index object.

link will have the following keys:

:ref_hash
:data_processing_hash
:(<sample>|<chromatogram>)_default_data_processing

may have:

:source_file_hash


31
32
33
34
35
# File 'lib/mspire/mzml/io_index.rb', line 31

def initialize(io, byte_index, link)
  @io, @byte_index, @link = io, byte_index, link
  @object_class = Mspire::Mzml.const_get(@byte_index.name.to_s.capitalize)
  @closetag_regexp = %r{</#{name}>}
end

Instance Attribute Details

#byte_indexObject (readonly)

Returns the value of attribute byte_index.



14
15
16
# File 'lib/mspire/mzml/io_index.rb', line 14

def byte_index
  @byte_index
end

#ioObject (readonly)

Returns the value of attribute io.



12
13
14
# File 'lib/mspire/mzml/io_index.rb', line 12

def io
  @io
end

hash of relevant hashes and objects for linking



17
18
19
# File 'lib/mspire/mzml/io_index.rb', line 17

def link
  @link
end

Instance Method Details

#[](index) ⇒ Object



48
49
50
# File 'lib/mspire/mzml/io_index.rb', line 48

def [](index)
  @object_class.from_xml(fetch_xml_node(index), @link)
end

#each(&block) ⇒ Object



41
42
43
44
45
46
# File 'lib/mspire/mzml/io_index.rb', line 41

def each(&block)
  return to_enum(__method__) unless block
  (0...byte_index.size).each do |int|
    block.call(self[int])
  end
end

#fetch_xml_node(index) ⇒ Object



73
74
75
# File 'lib/mspire/mzml/io_index.rb', line 73

def fetch_xml_node(index)
  xml_node_from_start_byte(byte_index[index])
end

#get_xml_string(start_byte) ⇒ Object

gets the data string through to last element



58
59
60
61
62
63
64
65
66
# File 'lib/mspire/mzml/io_index.rb', line 58

def get_xml_string(start_byte)
  @io.seek(start_byte)
  data = ""
  @io.each_line do |line|
    data << line 
    break if @closetag_regexp.match(line)
  end
  data
end

#lengthObject Also known as: size



52
53
54
# File 'lib/mspire/mzml/io_index.rb', line 52

def length
  @byte_index.length
end

#nameObject



37
38
39
# File 'lib/mspire/mzml/io_index.rb', line 37

def name
  @byte_index.name
end

#xml_node_from_start_byte(start_byte) ⇒ Object



68
69
70
71
# File 'lib/mspire/mzml/io_index.rb', line 68

def xml_node_from_start_byte(start_byte)
  xml = get_xml_string(start_byte)
  Nokogiri::XML.parse(xml, nil, @encoding, Parser::NOBLANKS).root
end