Class: DMAP::Element
- Inherits:
-
Object
- Object
- DMAP::Element
- 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
-
#real_class ⇒ Object
readonly
Returns the value of attribute real_class.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
- #close_stream ⇒ Object
-
#initialize(tag_or_dmap, new_content = nil) ⇒ Element
constructor
Accepts a string or an IO.
- #is_dmap? ⇒ Boolean
-
#to_dmap ⇒ Object
Adds the tag to a request to create the dmap.
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_class ⇒ Object (readonly)
Returns the value of attribute real_class.
13 14 15 |
# File 'lib/dmap.rb', line 13 def real_class @real_class end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
12 13 14 |
# File 'lib/dmap.rb', line 12 def tag @tag end |
Instance Method Details
#close_stream ⇒ Object
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
51 52 53 |
# File 'lib/dmap.rb', line 51 def is_dmap? true end |
#to_dmap ⇒ Object
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 |