Class: OrderedHash

Inherits:
Hash show all
Defined in:
lib/mongo/util/ordered_hash.rb

Overview

A hash in which the order of keys are preserved.

Under Ruby 1.9 and greater, this class has no added methods because Ruby’s Hash already keeps its keys ordered by order of insertion.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a, &b) ⇒ OrderedHash

Returns a new instance of OrderedHash.



52
53
54
55
# File 'lib/mongo/util/ordered_hash.rb', line 52

def initialize(*a, &b)
  super
  @ordered_keys = []
end

Instance Attribute Details

#ordered_keysObject

Returns the value of attribute ordered_keys.



35
36
37
# File 'lib/mongo/util/ordered_hash.rb', line 35

def ordered_keys
  @ordered_keys
end

Class Method Details

.[](*args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mongo/util/ordered_hash.rb', line 37

def self.[] *args
  oh = OrderedHash.new
  if Hash === args[0]
    oh.merge! args[0]
  elsif (args.size % 2) != 0
    raise ArgumentError, "odd number of elements for Hash"
  else
    0.step(args.size - 1, 2) do |key|
      value = key + 1
      oh[args[key]] = args[value]
    end
  end
  oh
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/mongo/util/ordered_hash.rb', line 23

def ==(other)
  begin
    !other.nil? &&
      keys == other.keys &&
      values == other.values
  rescue
    false
  end
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  @ordered_keys ||= []
  @ordered_keys << key unless @ordered_keys.include?(key)
  super(key, value)
end

#clearObject



117
118
119
120
# File 'lib/mongo/util/ordered_hash.rb', line 117

def clear
  super
  @ordered_keys = []
end

#delete(key, &block) ⇒ Object



104
105
106
107
# File 'lib/mongo/util/ordered_hash.rb', line 104

def delete(key, &block)
  @ordered_keys.delete(key) if @ordered_keys
  super
end

#delete_if(&block) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/mongo/util/ordered_hash.rb', line 109

def delete_if(&block)
  self.each { |k,v|
    if yield k, v
      delete(k)
    end
  }
end

#eachObject Also known as: each_pair



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

def each
  @ordered_keys ||= []
  @ordered_keys.each { |k| yield k, self[k] }
  self
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
# File 'lib/mongo/util/ordered_hash.rb', line 131

def eql?(o)
  if o.instance_of? OrderedHash
    self.hash == o.hash
  else
    false
  end
end

#hashObject



122
123
124
125
126
127
128
129
# File 'lib/mongo/util/ordered_hash.rb', line 122

def hash
  code = 17
  each_pair do |key, value|
    code = 37 * code + key.hash
    code = 37 * code + value.hash
  end
  code & 0x7fffffff
end

#inspectObject



98
99
100
101
102
# File 'lib/mongo/util/ordered_hash.rb', line 98

def inspect
  str = '{'
  str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
  str << '}'
end

#keysObject



57
58
59
# File 'lib/mongo/util/ordered_hash.rb', line 57

def keys
  @ordered_keys || []
end

#merge(other) ⇒ Object



83
84
85
86
87
# File 'lib/mongo/util/ordered_hash.rb', line 83

def merge(other)
  oh = self.dup
  oh.merge!(other)
  oh
end

#merge!(other) ⇒ Object Also known as: update



89
90
91
92
93
94
# File 'lib/mongo/util/ordered_hash.rb', line 89

def merge!(other)
  @ordered_keys ||= []
  @ordered_keys += other.keys # unordered if not an OrderedHash
  @ordered_keys.uniq!
  super(other)
end

#to_aObject



74
75
76
77
# File 'lib/mongo/util/ordered_hash.rb', line 74

def to_a
  @ordered_keys ||= []
  @ordered_keys.map { |k| [k, self[k]] }      
end

#valuesObject



79
80
81
# File 'lib/mongo/util/ordered_hash.rb', line 79

def values
  collect { |k, v| v }
end