Class: BSON::OrderedHash

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a, &b) ⇒ OrderedHash

Returns a new instance of OrderedHash.


55
56
57
58
# File 'lib/bson/ordered_hash.rb', line 55

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

Instance Attribute Details

#ordered_keysObject

Returns the value of attribute ordered_keys


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

def ordered_keys
  @ordered_keys
end

Class Method Details

.[](*args) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bson/ordered_hash.rb', line 40

def self.[] *args
  oh = BSON::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
32
33
34
# File 'lib/bson/ordered_hash.rb', line 23

def ==(other)
  begin
    case other
    when BSON::OrderedHash
       keys == other.keys && values == other.values
    else
      super
    end
  rescue
    false
  end
end

#[]=(key, value) ⇒ Object


69
70
71
72
73
74
# File 'lib/bson/ordered_hash.rb', line 69

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

#clearObject


154
155
156
157
# File 'lib/bson/ordered_hash.rb', line 154

def clear
  super
  @ordered_keys = []
end

#delete(key, &block) ⇒ Object


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

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

#delete_if(&block) ⇒ Object


128
129
130
131
132
133
134
135
# File 'lib/bson/ordered_hash.rb', line 128

def delete_if(&block)
  keys.each do |key|
    if yield key, self[key]
      delete(key)
    end
  end
  self
end

#dupObject


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

def dup
  result = OrderedHash.new
  @ordered_keys.each do |key|
    result[key] = self[key]
  end
  result
end

#eachObject Also known as: each_pair


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

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

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)

174
175
176
177
178
179
180
# File 'lib/bson/ordered_hash.rb', line 174

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

#hashObject


165
166
167
168
169
170
171
172
# File 'lib/bson/ordered_hash.rb', line 165

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

#initialize_copy(original) ⇒ Object


159
160
161
162
# File 'lib/bson/ordered_hash.rb', line 159

def initialize_copy(original)
  super
  @ordered_keys = original.ordered_keys.dup
end

#inspectObject


117
118
119
120
121
# File 'lib/bson/ordered_hash.rb', line 117

def inspect
  str = "#<BSON::OrderedHash:0x#{self.object_id.to_s(16)} {"
  str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
  str << '}>'
end

#keysObject


65
66
67
# File 'lib/bson/ordered_hash.rb', line 65

def keys
  @ordered_keys.dup
end

#merge(other) ⇒ Object


95
96
97
98
99
# File 'lib/bson/ordered_hash.rb', line 95

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

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


101
102
103
104
105
# File 'lib/bson/ordered_hash.rb', line 101

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

#reject(&block) ⇒ Object


137
138
139
140
141
# File 'lib/bson/ordered_hash.rb', line 137

def reject(&block)
  clone = self.clone
  return clone unless block_given?
  clone.delete_if(&block)
end

#reject!(&block) ⇒ Object


143
144
145
146
147
148
149
150
151
152
# File 'lib/bson/ordered_hash.rb', line 143

def reject!(&block)
  changed = false
  self.each do |k,v|
    if yield k, v
      changed = true
      delete(k)
    end
  end
  changed ? self : nil
end

#replace(other) ⇒ Object


90
91
92
93
# File 'lib/bson/ordered_hash.rb', line 90

def replace(other)
  @ordered_keys.replace(other.keys)
  super
end

#to_aObject


82
83
84
# File 'lib/bson/ordered_hash.rb', line 82

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

#valuesObject


86
87
88
# File 'lib/bson/ordered_hash.rb', line 86

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

#yaml_initialize(tag, val) ⇒ Object


60
61
62
63
# File 'lib/bson/ordered_hash.rb', line 60

def yaml_initialize(tag, val)
  @ordered_keys = []
  super
end