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!



23
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
# File 'lib/dmap.rb', line 23

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)
    @io = tag_or_dmap if new_content.nil?
  rescue NoMethodError
    @tag = tag_or_dmap[0..3]
  end
  
  # Find out the details of this tag
  begin
    @name,type = DMAP::TAGS[@tag.to_sym]
  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

#real_classObject (readonly)

Returns the value of attribute real_class.



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

def real_class
  @real_class
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

Instance Method Details

#close_streamObject



55
56
57
58
59
60
61
# File 'lib/dmap.rb', line 55

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

#is_dmap?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dmap.rb', line 51

def is_dmap?
  true
end

#to_dmapObject

Adds the tag to a request to create the dmap



64
65
66
67
68
69
70
71
# File 'lib/dmap.rb', line 64

def to_dmap
  begin
    "#{@tag.downcase}#{@real_class.to_dmap}"
  rescue
    warn("Error while putting #{@tag} to dmap")
    raise
  end
end