Class: RestrictedOrderedHash

Inherits:
ActiveSupport::OrderedHash
  • Object
show all
Defined in:
lib/helpers/restricted_ordered_hash.rb

Overview

Class RestrictedOrderedHash

this is a helper Class for ID3::Frame

this is a helper Class for GenericTag

this is from 2002 .. new Ruby Versions now have "OrderedHash" .. but I'll keep this class for now.

Direct Known Subclasses

ID3::Frame, ID3::GenericTag

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRestrictedOrderedHash

Returns a new instance of RestrictedOrderedHash.


19
20
21
22
# File 'lib/helpers/restricted_ordered_hash.rb', line 19

def initialize 
  @locked = false
  super
end

Instance Attribute Details

#lockedObject

Returns the value of attribute locked


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

def locked
  @locked
end

Instance Method Details

#[]=(key, val) ⇒ Object


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

def []= (key,val)
  if self[key]    #            self.old_store(key,val)    # this would overwrite the old_value if a key already exists (duplicate ID3-Frames)

    
    # strictly speaking, we only need this for the ID3v2 Tag class Tag2:
    if self[key].class != ID3::FrameArray   # Make this ID3::FrameArray < Array
      old_value = self[key]
      new_value = ID3::FrameArray.new
      new_value << old_value           # make old_value a FrameArray
      self.old_store(key, new_value  )
    end
    self[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 
      self.old_store(key,val)
    end
  end
end

#delete(key) ⇒ Object


54
55
56
57
58
# File 'lib/helpers/restricted_ordered_hash.rb', line 54

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

#lockObject


15
16
17
# File 'lib/helpers/restricted_ordered_hash.rb', line 15

def lock
  @locked = true
end

#old_deleteObject

users can not delete entries from a locked hash..


52
# File 'lib/helpers/restricted_ordered_hash.rb', line 52

alias old_delete delete

#old_storeObject


24
# File 'lib/helpers/restricted_ordered_hash.rb', line 24

alias old_store []=