Class: ActiveSupport::OrderedHash
- Inherits:
-
Hash
show all
- Defined in:
- lib/active_support/ordered_hash.rb
Overview
Constant Summary
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, #except, #slice, #to_json
#except, #except!
#slice, #slice!
#diff
included, #rename_key, #to_query, #to_xml
#reverse_merge, #reverse_merge!
#deep_merge, #deep_merge!
#with_indifferent_access
#assert_valid_keys, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!
Constructor Details
#initialize(*args, &block) ⇒ OrderedHash
Returns a new instance of OrderedHash.
26
27
28
29
|
# File 'lib/active_support/ordered_hash.rb', line 26
def initialize(*args, &block)
super
@keys = []
end
|
Class Method Details
.[](*args) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/active_support/ordered_hash.rb', line 31
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
61
62
63
64
|
# File 'lib/active_support/ordered_hash.rb', line 61
def []=(key, value)
@keys << key if !has_key?(key)
super
end
|
120
121
122
123
124
|
# File 'lib/active_support/ordered_hash.rb', line 120
def clear
super
@keys.clear
self
end
|
#delete(key) ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/active_support/ordered_hash.rb', line 66
def delete(key)
if has_key? key
index = @keys.index(key)
@keys.delete_at index
end
super
end
|
#delete_if ⇒ Object
74
75
76
77
78
|
# File 'lib/active_support/ordered_hash.rb', line 74
def delete_if
super
sync_keys!
self
end
|
#each ⇒ Object
Also known as:
each_pair
114
115
116
|
# File 'lib/active_support/ordered_hash.rb', line 114
def each
@keys.each {|key| yield [key, self[key]]}
end
|
106
107
108
|
# File 'lib/active_support/ordered_hash.rb', line 106
def each_key
@keys.each { |key| yield key }
end
|
#each_value ⇒ Object
110
111
112
|
# File 'lib/active_support/ordered_hash.rb', line 110
def each_value
@keys.each { |key| yield self[key]}
end
|
#initialize_copy(other) ⇒ Object
55
56
57
58
59
|
# File 'lib/active_support/ordered_hash.rb', line 55
def initialize_copy(other)
super
@keys = other.keys
end
|
148
149
150
|
# File 'lib/active_support/ordered_hash.rb', line 148
def inspect
"#<OrderedHash #{super}>"
end
|
90
91
92
|
# File 'lib/active_support/ordered_hash.rb', line 90
def keys
@keys.dup
end
|
#merge(other_hash) ⇒ Object
137
138
139
|
# File 'lib/active_support/ordered_hash.rb', line 137
def merge(other_hash)
dup.merge!(other_hash)
end
|
#merge!(other_hash) ⇒ Object
132
133
134
135
|
# File 'lib/active_support/ordered_hash.rb', line 132
def merge!(other_hash)
other_hash.each {|k,v| self[k] = v }
self
end
|
#reject(&block) ⇒ Object
86
87
88
|
# File 'lib/active_support/ordered_hash.rb', line 86
def reject(&block)
dup.reject!(&block)
end
|
80
81
82
83
84
|
# File 'lib/active_support/ordered_hash.rb', line 80
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.
142
143
144
145
146
|
# File 'lib/active_support/ordered_hash.rb', line 142
def replace(other)
super
@keys = other.keys
self
end
|
126
127
128
129
130
|
# File 'lib/active_support/ordered_hash.rb', line 126
def shift
k = @keys.first
v = delete(k)
[k, v]
end
|
102
103
104
|
# File 'lib/active_support/ordered_hash.rb', line 102
def to_a
@keys.map { |key| [ key, self[key] ] }
end
|
98
99
100
|
# File 'lib/active_support/ordered_hash.rb', line 98
def to_hash
self
end
|
#to_yaml(opts = {}) ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/active_support/ordered_hash.rb', line 14
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_type ⇒ Object
10
11
12
|
# File 'lib/active_support/ordered_hash.rb', line 10
def to_yaml_type
"!tag:yaml.org,2002:omap"
end
|
94
95
96
|
# File 'lib/active_support/ordered_hash.rb', line 94
def values
@keys.collect { |key| self[key] }
end
|