Class: ActiveSupport::OrderedHash

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

Overview

:nodoc:

Direct Known Subclasses

OrderedOptions

Constant Summary

Constants included from CoreExtensions::Hash::Conversions

CoreExtensions::Hash::Conversions::XML_FORMATTING, CoreExtensions::Hash::Conversions::XML_PARSING, CoreExtensions::Hash::Conversions::XML_TYPE_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#as_json, #to_json

Methods included from CoreExtensions::Hash::Except

#except, #except!

Methods included from CoreExtensions::Hash::Slice

#slice, #slice!

Methods included from CoreExtensions::Hash::Diff

#diff

Methods included from CoreExtensions::Hash::Conversions

included, #rename_key, #to_query, #to_xml

Methods included from CoreExtensions::Hash::ReverseMerge

#reverse_merge, #reverse_merge!

Methods included from CoreExtensions::Hash::DeepMerge

#deep_merge, #deep_merge!

Methods included from CoreExtensions::Hash::IndifferentAccess

#with_indifferent_access

Methods included from CoreExtensions::Hash::Keys

#assert_valid_keys, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!

Constructor Details

#initialize(*args, &block) ⇒ OrderedHash

Returns a new instance of OrderedHash.



8
9
10
11
# File 'lib/active_support/ordered_hash.rb', line 8

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

Class Method Details

.[](*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_support/ordered_hash.rb', line 13

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



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

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

#clearObject



102
103
104
105
106
# File 'lib/active_support/ordered_hash.rb', line 102

def clear
  super
  @keys.clear
  self
end

#delete(key) ⇒ Object



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

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

#delete_ifObject



56
57
58
59
60
# File 'lib/active_support/ordered_hash.rb', line 56

def delete_if
  super
  sync_keys!
  self
end

#eachObject Also known as: each_pair



96
97
98
# File 'lib/active_support/ordered_hash.rb', line 96

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

#each_keyObject



88
89
90
# File 'lib/active_support/ordered_hash.rb', line 88

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

#each_valueObject



92
93
94
# File 'lib/active_support/ordered_hash.rb', line 92

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

#initialize_copy(other) ⇒ Object



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

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

#inspectObject



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

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

#keysObject



72
73
74
# File 'lib/active_support/ordered_hash.rb', line 72

def keys
  @keys.dup
end

#merge(other_hash) ⇒ Object



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

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

#merge!(other_hash) ⇒ Object



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

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

#reject(&block) ⇒ Object



68
69
70
# File 'lib/active_support/ordered_hash.rb', line 68

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

#reject!Object



62
63
64
65
66
# File 'lib/active_support/ordered_hash.rb', line 62

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.



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

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

#shiftObject



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

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

#to_aObject



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

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

#to_hashObject



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

def to_hash
  self
end

#valuesObject



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

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