Class: DMAP::Element

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

Overview

Represents any DMAP tag and its content. Will have methods appropriate to the dmap specified

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_or_dmap, new_content = nil) ⇒ Element

Accepts a string or an IO. The first four bytes (in either case) should be the tag you wish to make.

If you have a big dmap file this is your lucky day, this class only processes the parts needed for any queries you’re making, so its all on-the-fly.

NB. if you specify ‘content` while passing a dmap tag you will overwrite anything that was in the dmap!



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dmap.rb', line 24

def initialize(tag_or_dmap,new_content = nil)      
  # Assume we have an IO object, if this fails then we probably have a string instead
  begin
    @tag = tag_or_dmap.read(4).upcase
    @unparsed_content = true
    @io = tag_or_dmap if new_content.nil?
  rescue NoMethodError
    @tag = tag_or_dmap[0..3].upcase
  end
  
  # Find out the details of this tag
  begin
    type,@name = DMAP.const_get(@tag)
  rescue NameError
    raise NameError, "I don't know how to interpret the tag '#{@tag}'. Please extend the DMAP module!"
  end
  
  self.send("parse_#{type}",new_content)
  fudge = @real_class
  eigenclass = class<<self; self; end
  eigenclass.class_eval do
    extend Forwardable
    def_delegators :@real_class,*fudge.methods - ['__send__','__id__','to_dmap']
    def inspect
      "<#{@tag}: #{@real_class.inspect}>"
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/dmap.rb', line 13

def name
  @name
end

#real_classObject (readonly)

Returns the value of attribute real_class.



14
15
16
# File 'lib/dmap.rb', line 14

def real_class
  @real_class
end

#unparsed_contentObject (readonly)

Returns the value of attribute unparsed_content.



12
13
14
# File 'lib/dmap.rb', line 12

def unparsed_content
  @unparsed_content
end

Instance Method Details

#close_streamObject



53
54
55
56
57
58
59
# File 'lib/dmap.rb', line 53

def close_stream
  begin
    @io.close
  rescue NoMethodError
    # There was no IO or its already been closed
  end
end

#to_dmapObject

Adds the tag to a request to create the dmap



62
63
64
# File 'lib/dmap.rb', line 62

def to_dmap
  "#{@tag.downcase}#{@real_class.to_dmap}"
end