Class: Cassandra::OrderedHash

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

Overview

Copyright © 2004-2009 David Heinemeier Hansson

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ OrderedHash

Returns a new instance of OrderedHash.



37
38
39
40
# File 'lib/cassandra/ordered_hash.rb', line 37

def initialize(*args, &block)
  super
  @keys = []
end

Class Method Details

.[](*array) ⇒ Object



31
32
33
34
35
# File 'lib/cassandra/ordered_hash.rb', line 31

def self.[](*array)
  hash = new
  array.each_slice(2) { |key, value| hash[key] = value }
  hash
end

Instance Method Details

#[]=(key, value) ⇒ Object



48
49
50
51
# File 'lib/cassandra/ordered_hash.rb', line 48

def []=(key, value)
  @keys << key if !has_key?(key)
  super
end

#clearObject



103
104
105
106
107
# File 'lib/cassandra/ordered_hash.rb', line 103

def clear
  super
  @keys.clear
  self
end

#delete(key) ⇒ Object



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

def delete(key)
  if has_key? key
    index = @keys.index(key)
    @keys.delete_at index
  end
  super
end

#delete_ifObject



61
62
63
64
65
# File 'lib/cassandra/ordered_hash.rb', line 61

def delete_if
  super
  sync_keys!
  self
end

#eachObject Also known as: each_pair



97
98
99
# File 'lib/cassandra/ordered_hash.rb', line 97

def each
  @keys.each {|key| yield [key, self[key]]}
end

#each_keyObject



89
90
91
# File 'lib/cassandra/ordered_hash.rb', line 89

def each_key
  @keys.each { |key| yield key }
end

#each_valueObject



93
94
95
# File 'lib/cassandra/ordered_hash.rb', line 93

def each_value
  @keys.each { |key| yield self[key]}
end

#initialize_copy(other) ⇒ Object



42
43
44
45
46
# File 'lib/cassandra/ordered_hash.rb', line 42

def initialize_copy(other)
  super
  # make a deep copy of keys
  @keys = other.keys
end

#inspectObject



124
125
126
# File 'lib/cassandra/ordered_hash.rb', line 124

def inspect
  "#<OrderedHash #{super}>"
end

#keysObject



77
78
79
# File 'lib/cassandra/ordered_hash.rb', line 77

def keys
  @keys.dup
end

#merge(other_hash) ⇒ Object



120
121
122
# File 'lib/cassandra/ordered_hash.rb', line 120

def merge(other_hash)
  dup.merge!(other_hash)
end

#merge!(other_hash) ⇒ Object



115
116
117
118
# File 'lib/cassandra/ordered_hash.rb', line 115

def merge!(other_hash)
  other_hash.each {|k,v| self[k] = v }
  self
end

#reject(&block) ⇒ Object



73
74
75
# File 'lib/cassandra/ordered_hash.rb', line 73

def reject(&block)
  dup.reject!(&block)
end

#reject!Object



67
68
69
70
71
# File 'lib/cassandra/ordered_hash.rb', line 67

def reject!
  super
  sync_keys!
  self
end

#shiftObject



109
110
111
112
113
# File 'lib/cassandra/ordered_hash.rb', line 109

def shift
  k = @keys.first
  v = delete(k)
  [k, v]
end

#to_hashObject



85
86
87
# File 'lib/cassandra/ordered_hash.rb', line 85

def to_hash
  self
end

#valuesObject



81
82
83
# File 'lib/cassandra/ordered_hash.rb', line 81

def values
  @keys.collect { |key| self[key] }
end