Class: DMAP::Array

Inherits:
Array
  • Object
show all
Defined in:
lib/dmap.rb

Overview

We may not always want to parse an entire DMAP in one go, here we extend Array so that we can hold the reference to the io and the point at which the data starts, so we can parse it later if the contents are requested

Constant Summary collapse

@@parse_immediately =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array_or_io) ⇒ Array

Returns a new instance of Array.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/dmap.rb', line 162

def initialize(array_or_io)
  original_new
  begin
    # Lets assume its an io
    @dmap_length = array_or_io.read(4).unpack("N")[0]
    @dmap_io     = array_or_io
    @dmap_start  = @dmap_io.tell
    @unparsed_data = true
  rescue NoMethodError
    begin
      array_or_io.each do |element|
        if element.is_dmap?
          self.push element
        else
          raise "Thisneeds to be a DMAP::Element. #{element.inspect}"
        end
      end
    rescue NoMethodError
    end
    @unparsed_data = false
  end
  
  parse_dmap if @@parse_immediately
end

Instance Attribute Details

#unparsed_dataObject (readonly)

Returns the value of attribute unparsed_data.



150
151
152
# File 'lib/dmap.rb', line 150

def unparsed_data
  @unparsed_data
end

Class Method Details

.parse_immediatelyObject



153
154
155
# File 'lib/dmap.rb', line 153

def self.parse_immediately
  @@parse_immediately
end

.parse_immediately=(bool) ⇒ Object



157
158
159
# File 'lib/dmap.rb', line 157

def self.parse_immediately=(bool)
  @@parse_immediately = (bool == true)
end

Instance Method Details

#inspectObject



205
206
207
208
209
210
211
# File 'lib/dmap.rb', line 205

def inspect
  if not @unparsed_data
    super
  else
    "Some unparsed DMAP elements"
  end
end

#original_newObject



161
# File 'lib/dmap.rb', line 161

alias :original_new :initialize

#parse_dmapObject

Parse any unparsed dmap data stored, and add the elements to the array



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/dmap.rb', line 214

def parse_dmap
  return if not @unparsed_data
  
  # Remember the position of the IO head so we can put it back later
  io_position = @dmap_io.tell
  
  # Go to the begining of the list
  @dmap_io.seek(@dmap_start)
  # Enumerate all tags in this list
  while @dmap_io.tell < (@dmap_start + @dmap_length)
    self.push Element.new(@dmap_io)
  end
  
  # Return the IO head to where it was
  @dmap_io.seek(io_position)
  @unparsed_data = false
end

#to_dmapObject



195
196
197
198
199
200
201
202
203
# File 'lib/dmap.rb', line 195

def to_dmap
  out = "\000\000\000\000"
  (0...self.length).to_a.each do |n|
    out << self[n].to_dmap
  end
  
  out[0..3] = [out.length - 4].pack("N")
  out
end