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.



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

def initialize(array_or_io)
  original_new
  begin
    # Lets assume its an io
    @dmap_length = array_or_io.read(4).unpack("N")[0] # FIXME: Should be Q? but that's not working?
    @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|
        self.push element
      end
    rescue NoMethodError
    end
    @unparsed_data = false
  end
  
end

Instance Attribute Details

#parse_immediatelyObject

Returns the value of attribute parse_immediately.



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

def parse_immediately
  @parse_immediately
end

#unparsed_dataObject (readonly)

Returns the value of attribute unparsed_data.



130
131
132
# File 'lib/dmap.rb', line 130

def unparsed_data
  @unparsed_data
end

Class Method Details

.parse_immediatelyObject



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

def self.parse_immediately
  @@parse_immediately
end

.parse_immediately=(bool) ⇒ Object



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

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

Instance Method Details

#inspectObject



182
183
184
185
186
187
188
# File 'lib/dmap.rb', line 182

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

#original_newObject



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

alias :original_new :initialize

#parse_dmapObject

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/dmap.rb', line 191

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



172
173
174
175
176
177
178
179
180
# File 'lib/dmap.rb', line 172

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