Class: Vitae::OrderedHash

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

Overview

This OrderedHash class was stolen from ActiveSupport. The order of iteration over hashes in Ruby 1.8 is undefined. For example, you do not know the order in which keys will return keys, or each yield pairs. Vitae::OrderedHash implements a hash that preserves insertion order, as in Ruby 1.9:

oh = Vitae::OrderedHash.new
oh[:a] = 1
oh[:b] = 2
oh.keys # => [:a, :b], this order is guaranteed

Vitae::OrderedHash is namespaced to prevent conflicts with other implementations.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ OrderedHash

In MRI the Hash class is core and written in C. In particular, methods are programmed with explicit C function calls and polymorphism is not honored.

For example, []= is crucial in this implementation to maintain the @keys array but hash.c invokes rb_hash_aset() originally. This prevents method reuse through inheritance and forces us to reimplement stuff.

For instance, we cannot use the inherited #merge! because albeit the algorithm itself would work, our []= is not being called at all by the C code.



47
48
49
50
# File 'lib/vitae/ordered_hash.rb', line 47

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

Class Method Details

.[](*args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vitae/ordered_hash.rb', line 52

def self.[](*args)
  ordered_hash = new

  if (args.length == 1 && args.first.is_a?(Array))
    args.first.each do |key_value_pair|
      next unless (key_value_pair.is_a?(Array))
      ordered_hash[key_value_pair[0]] = key_value_pair[1]
    end

    return ordered_hash
  end

  unless (args.size % 2 == 0)
    raise ArgumentError.new("odd number of arguments for Hash")
  end

  args.each_with_index do |val, ind|
    next if (ind % 2 != 0)
    ordered_hash[val] = args[ind + 1]
  end

  ordered_hash
end

Instance Method Details

#[]=(key, value) ⇒ Object



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

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

#clearObject



141
142
143
144
145
# File 'lib/vitae/ordered_hash.rb', line 141

def clear
  super
  @keys.clear
  self
end

#delete(key) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/vitae/ordered_hash.rb', line 87

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

#delete_ifObject



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

def delete_if
  super
  sync_keys!
  self
end

#eachObject Also known as: each_pair



135
136
137
# File 'lib/vitae/ordered_hash.rb', line 135

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

#each_keyObject



127
128
129
# File 'lib/vitae/ordered_hash.rb', line 127

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

#each_valueObject



131
132
133
# File 'lib/vitae/ordered_hash.rb', line 131

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

#initialize_copy(other) ⇒ Object



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

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

#inspectObject



179
180
181
# File 'lib/vitae/ordered_hash.rb', line 179

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

#invertObject



175
176
177
# File 'lib/vitae/ordered_hash.rb', line 175

def invert
  OrderedHash[self.to_a.map!{|key_value_pair| key_value_pair.reverse}]
end

#keysObject



111
112
113
# File 'lib/vitae/ordered_hash.rb', line 111

def keys
  @keys.dup
end

#merge(other_hash, &block) ⇒ Object



164
165
166
# File 'lib/vitae/ordered_hash.rb', line 164

def merge(other_hash, &block)
  dup.merge!(other_hash, &block)
end

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



153
154
155
156
157
158
159
160
# File 'lib/vitae/ordered_hash.rb', line 153

def merge!(other_hash)
  if block_given?
    other_hash.each { |k, v| self[k] = key?(k) ? yield(k, self[k], v) : v }
  else
    other_hash.each { |k, v| self[k] = v }
  end
  self
end

#reject(&block) ⇒ Object



107
108
109
# File 'lib/vitae/ordered_hash.rb', line 107

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

#reject!Object



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

def reject!
  super
  sync_keys!
  self
end

#replace(other) ⇒ Object

When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.



169
170
171
172
173
# File 'lib/vitae/ordered_hash.rb', line 169

def replace(other)
  super
  @keys = other.keys
  self
end

#shiftObject



147
148
149
150
151
# File 'lib/vitae/ordered_hash.rb', line 147

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

#to_aObject



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

def to_a
  @keys.map { |key| [ key, self[key] ] }
end

#to_hashObject



119
120
121
# File 'lib/vitae/ordered_hash.rb', line 119

def to_hash
  self
end

#to_yaml(opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/vitae/ordered_hash.rb', line 24

def to_yaml(opts = {})
  YAML.quick_emit(self, opts) do |out|
    out.seq(taguri, to_yaml_style) do |seq|
      each do |k, v|
        seq.add(k => v)
      end
    end
  end
end

#to_yaml_typeObject

:nodoc:



20
21
22
# File 'lib/vitae/ordered_hash.rb', line 20

def to_yaml_type
  "!tag:yaml.org,2002:omap"
end

#valuesObject



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

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