Class: ID3::RestrictedOrderedHash

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

Overview

Class RestrictedOrderedHash

Direct Known Subclasses

Frame

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#inverse, #invert, #old_invert

Constructor Details

#initializeRestrictedOrderedHash

Returns a new instance of RestrictedOrderedHash.



676
677
678
679
680
681
# File 'lib/id3.rb', line 676

def initialize 
  @locked = false
  @count  = 0
  @order  = []
  super
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



670
671
672
# File 'lib/id3.rb', line 670

def count
  @count
end

#lockedObject

Returns the value of attribute locked.



670
671
672
# File 'lib/id3.rb', line 670

def locked
  @locked
end

#orderObject

Returns the value of attribute order.



670
671
672
# File 'lib/id3.rb', line 670

def order
  @order
end

Instance Method Details

#[]=(key, val) ⇒ Object



685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/id3.rb', line 685

def []= (key,val)
  if self[key]
     self.old_store(key,val) 
  else
     if @locked
        # we're not allowed to add new keys!
       raise ArgumentError, "You can not add new keys! The ID3-frame #{@name} has fixed entries!\n" +
       "               valid key are: " + self.keys.join(",") +"\n"

     else 
        @count += 1
        @order += [key]
        self.old_store(key,val)
     end
  end
end

#delete(key) ⇒ Object



729
730
731
732
733
734
# File 'lib/id3.rb', line 729

def delete (key)
   if !@locked
      old_delete(key)
      @order.delete(key)
   end
end

#inspectObject

returns the human-readable ordered hash in correct order .. ;-)



712
713
714
715
716
717
718
719
720
721
722
723
# File 'lib/id3.rb', line 712

def inspect
   first = true
   str = "{"
   self.order.each{ |key|
     str += ", " if !first
     str += key.inspect
     str += "=>"
     str += (self[key]).inspect
     first = false
   }
   str +="}"
end

#lockObject



672
673
674
# File 'lib/id3.rb', line 672

def lock
  @locked = true
end

#old_deleteObject

users can not delete entries from a locked hash..



727
# File 'lib/id3.rb', line 727

alias old_delete delete

#old_storeObject



683
# File 'lib/id3.rb', line 683

alias old_store []=

#valuesObject



702
703
704
705
706
707
708
# File 'lib/id3.rb', line 702

def values
  array = []
  @order.each { |key|
     array.push self[key]
  }
  array
end