Class: Ms::Mascot::Dat::Archive

Inherits:
ExternalArchive
  • Object
show all
Includes:
Utils
Defined in:
lib/ms/mascot/dat/archive.rb

Overview

Provides access to a Mascot dat file.

Defined Under Namespace

Modules: Utils

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

content_type_class, parse_content_type, parse_metadata

Constructor Details

#initialize(io = nil, io_index = nil) ⇒ Archive

Returns a new instance of Archive.



95
96
97
98
99
# File 'lib/ms/mascot/dat/archive.rb', line 95

def initialize(io=nil, io_index=nil)
  super(io)
  @metadata = (io)
  @section_names = []
end

Instance Attribute Details

#metadataObject (readonly)

A hash of metadata associated with this dat file.



93
94
95
# File 'lib/ms/mascot/dat/archive.rb', line 93

def 
  @metadata
end

Instance Method Details

#boundaryObject

The boundary separating sections, typically ‘–gc0p4Jq0M2Yt08jU534c0p’.



102
103
104
# File 'lib/ms/mascot/dat/archive.rb', line 102

def boundary
  "--#{[:boundary]}"
end

#each_query(&block) ⇒ Object



166
167
168
169
170
# File 'lib/ms/mascot/dat/archive.rb', line 166

def each_query(&block)
  section('index').queries.each do |key|
    block.call( self.section(key) )
  end
end

#query(num) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/ms/mascot/dat/archive.rb', line 172

def query(num)
  if si = section_index("query#{num}")
    self[si]
  else
    nil
  end
end

#reindex(&block) ⇒ Object

Reindexes self.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ms/mascot/dat/archive.rb', line 107

def reindex(&block)
  @section_names.clear
  reindex_by_sep(boundary, 
    :entry_follows_sep => true, 
    :exclude_sep => true,
  &block)

  # remove the first and last entries, which contain
  # the metadata and indicate the end of the multipart 
  # form data.
  io_index.shift
  io_index.pop

  self
end

#section(name) ⇒ Object

Returns the entry for the named section.



145
146
147
# File 'lib/ms/mascot/dat/archive.rb', line 145

def section(name)
  self[section_index(name)]
end

#section_index(name) ⇒ Object

Returns the index of the named section.



150
151
152
153
154
155
# File 'lib/ms/mascot/dat/archive.rb', line 150

def section_index(name)
  0.upto(length - 1) do |index|
    return index if section_name(index) == name
  end
  nil
end

#section_name(index) ⇒ Object

Returns the section name for the entry at index.



158
159
160
161
162
163
164
# File 'lib/ms/mascot/dat/archive.rb', line 158

def section_name(index)
  # all sections must be resolved for negative indicies to
  # work correctly (since otherwise @section_names may not
  # have the same length as self)
  resolve_sections if index < 0
  @section_names[index] ||= parse_section_name(index)
end

#section_names(resolve = true) ⇒ Object

The section names corresponding to each entry in self.

Normally section names are lazily parsed from the Content-Type header of an entry as needed. If resolve is true, all section names are parsed and then returned; otherwise section_names may return a partially-filled array.



139
140
141
142
# File 'lib/ms/mascot/dat/archive.rb', line 139

def section_names(resolve=true)
  resolve_sections if resolve
  @section_names
end

#str_to_entry(str) ⇒ Object

Converts str into an entry according to the content type header which should be present at the start of the string.



125
126
127
128
129
130
131
# File 'lib/ms/mascot/dat/archive.rb', line 125

def str_to_entry(str)
  if ctc = content_type_class(parse_content_type(str))
    ctc.parse(str)
  else
    str
  end
end