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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/dmap.rb', line 144

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
    parse_dmap if @@parse_immediately
  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
  
end

Instance Attribute Details

#unparsed_dataObject (readonly)

Returns the value of attribute unparsed_data.



132
133
134
# File 'lib/dmap.rb', line 132

def unparsed_data
  @unparsed_data
end

Class Method Details

.parse_immediatelyObject



135
136
137
# File 'lib/dmap.rb', line 135

def self.parse_immediately
  @@parse_immediately
end

.parse_immediately=(bool) ⇒ Object



139
140
141
# File 'lib/dmap.rb', line 139

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

Instance Method Details

#inspectObject



187
188
189
190
191
192
193
# File 'lib/dmap.rb', line 187

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

#original_newObject



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

alias :original_new :initialize

#parse_dmapObject

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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dmap.rb', line 196

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



177
178
179
180
181
182
183
184
185
# File 'lib/dmap.rb', line 177

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