Class: ActiveSupport::OrderedHash

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

Overview

:nodoc:

Direct Known Subclasses

OrderedOptions

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#as_json, #assert_valid_keys, #deep_merge, #deep_merge!, #diff, #encode_json, #except, #except!, #extract!, #extractable_options?, from_xml, #reverse_merge, #reverse_merge!, #slice, #slice!, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!, #to_param, #to_xml, #with_indifferent_access

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.



50
51
52
53
# File 'lib/active_support/ordered_hash.rb', line 50

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

Class Method Details

.[](*args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_support/ordered_hash.rb', line 55

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



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

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

#clearObject



144
145
146
147
148
# File 'lib/active_support/ordered_hash.rb', line 144

def clear
  super
  @keys.clear
  self
end

#delete(key) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/active_support/ordered_hash.rb', line 90

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

#delete_ifObject



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

def delete_if
  super
  sync_keys!
  self
end

#eachObject Also known as: each_pair



138
139
140
# File 'lib/active_support/ordered_hash.rb', line 138

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

#each_keyObject



130
131
132
# File 'lib/active_support/ordered_hash.rb', line 130

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

#each_valueObject



134
135
136
# File 'lib/active_support/ordered_hash.rb', line 134

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

#encode_with(coder) ⇒ Object



19
20
21
# File 'lib/active_support/ordered_hash.rb', line 19

def encode_with(coder)
  coder.represent_seq '!omap', map { |k,v| { k => v } }
end

#initialize_copy(other) ⇒ Object



79
80
81
82
83
# File 'lib/active_support/ordered_hash.rb', line 79

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

#inspectObject



182
183
184
# File 'lib/active_support/ordered_hash.rb', line 182

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

#invertObject



178
179
180
# File 'lib/active_support/ordered_hash.rb', line 178

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

#keysObject



114
115
116
# File 'lib/active_support/ordered_hash.rb', line 114

def keys
  @keys.dup
end

#merge(other_hash, &block) ⇒ Object



167
168
169
# File 'lib/active_support/ordered_hash.rb', line 167

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

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



156
157
158
159
160
161
162
163
# File 'lib/active_support/ordered_hash.rb', line 156

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



110
111
112
# File 'lib/active_support/ordered_hash.rb', line 110

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

#reject!Object



104
105
106
107
108
# File 'lib/active_support/ordered_hash.rb', line 104

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.



172
173
174
175
176
# File 'lib/active_support/ordered_hash.rb', line 172

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

#shiftObject



150
151
152
153
154
# File 'lib/active_support/ordered_hash.rb', line 150

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

#to_aObject



126
127
128
# File 'lib/active_support/ordered_hash.rb', line 126

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

#to_hashObject



122
123
124
# File 'lib/active_support/ordered_hash.rb', line 122

def to_hash
  self
end

#to_yaml(opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_support/ordered_hash.rb', line 23

def to_yaml(opts = {})
  if YAML.const_defined?(:ENGINE) && !YAML::ENGINE.syck?
    return super
  end

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

#to_yaml_typeObject



15
16
17
# File 'lib/active_support/ordered_hash.rb', line 15

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

#valuesObject



118
119
120
# File 'lib/active_support/ordered_hash.rb', line 118

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