Class: Mspire::Mzml::IOIndexableList

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/mspire/mzml/io_indexable_list.rb

Overview

An IOIndexableList is the base object for SpectrumList and ChromatogramList. It’s main feature is that it delegates all of its duties to the array like object.

Direct Known Subclasses

ChromatogramList, SpectrumList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_data_processing, array_like = [], id_to_index = nil) ⇒ IOIndexableList

array_like must implement #[] (with an Integer index), #each, size and length. For example, it may be an actual Array object, or it may be an IOIndex, something that behaves similar to an array but is really pulling objects by reading an io object. Sets the spectrum_list attribute of array_like if it can be set.



23
24
25
26
27
28
29
30
# File 'lib/mspire/mzml/io_indexable_list.rb', line 23

def initialize(default_data_processing, array_like=[], id_to_index=nil)
  if array_like.respond_to?(:spectrum_list=)
    array_like.spectrum_list = self
  end
  @id_to_index = id_to_index
  @default_data_processing = default_data_processing
  __setobj__(array_like)
end

Instance Attribute Details

#default_data_processingObject

Returns the value of attribute default_data_processing.



13
14
15
# File 'lib/mspire/mzml/io_indexable_list.rb', line 13

def default_data_processing
  @default_data_processing
end

#id_to_indexObject

a hash linking an id to the Integer index



16
17
18
# File 'lib/mspire/mzml/io_indexable_list.rb', line 16

def id_to_index
  @id_to_index
end

Instance Method Details

#[](arg) ⇒ Object

arg may be an Integer or a String (an id)



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

def [](arg)
  arg.is_a?(Integer) ? get_delegate[arg] : get_delegate[ @id_to_index[arg] ]
end

#create_id_to_index!Object

method to generate the id_to_index hash from the underlying delegated object.



43
44
45
46
47
48
49
# File 'lib/mspire/mzml/io_indexable_list.rb', line 43

def create_id_to_index!
  @id_to_index = {}
  get_delegate.each_with_index do |obj, i|
    @id_to_index[obj.id] = i
  end
  @id_to_index
end

#list_typeObject

for a class like <Object>List, returns :object. So a SpectrumList will return :spectrum.



34
35
36
37
38
# File 'lib/mspire/mzml/io_indexable_list.rb', line 34

def list_type
  base = self.class.to_s.split('::').last.sub(/List$/,'')
  base[0] = base[0].downcase
  base.to_sym
end

#to_xml(builder, default_ids) ⇒ Object



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

def to_xml(builder, default_ids)
  default_ids["#{list_type}_data_processing".to_sym] = @default_data_processing.id
  xml_name = self.class.to_s.split('::').last
  xml_name[0] = xml_name[0].downcase
  builder.tag!(xml_name.to_sym, count: self.size, defaultDataProcessingRef: @default_data_processing.id) do |iol_n|
    self.each_with_index do |obj,i|
      obj.index = i unless obj.index
      obj.to_xml(iol_n, default_ids)
    end
  end
  builder
end