Class: OrderedHash
- Inherits:
-
Hash
show all
- Defined in:
- lib/nanoc/base/ordered_hash.rb
Overview
AUTHOR
jan molic /mig/at/1984/dot/cz/
DESCRIPTION
Hash with preserved order and some array-like extensions
Public domain.
THANKS
Andrew Johnson for his suggestions and fixes of Hash[],
merge, to_a, inspect and shift
Instance Attribute Summary (collapse)
Class Method Summary
(collapse)
Instance Method Summary
(collapse)
#checksum, #freeze_recursively, #stringify_keys, #stringify_keys_recursively, #symbolize_keys, #symbolize_keys_recursively
Constructor Details
- (OrderedHash) initialize(*a, &b)
Returns a new instance of OrderedHash
31
32
33
34
|
# File 'lib/nanoc/base/ordered_hash.rb', line 31
def initialize(*a, &b)
super
@order = []
end
|
Instance Attribute Details
- (Object) order
Returns the value of attribute order
13
14
15
|
# File 'lib/nanoc/base/ordered_hash.rb', line 13
def order
@order
end
|
- (Object) to_yaml_style
Returns the value of attribute to_yaml_style
164
165
166
|
# File 'lib/nanoc/base/ordered_hash.rb', line 164
def to_yaml_style
@to_yaml_style
end
|
Class Method Details
+ (Object) [](*args)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/nanoc/base/ordered_hash.rb', line 16
def [] *args
hsh = OrderedHash.new
if Hash === args[0]
hsh.replace args[0]
elsif (args.size % 2) != 0
raise ArgumentError, "odd number of elements for Hash"
else
0.step(args.size - 1, 2) do |a|
b = a + 1
hsh[args[a]] = args[b]
end
end
hsh
end
|
Instance Method Details
- (Object) ==(hsh2)
44
45
46
47
|
# File 'lib/nanoc/base/ordered_hash.rb', line 44
def == hsh2
return false if @order != hsh2.order
super hsh2
end
|
- (Object) __class__
160
161
162
|
# File 'lib/nanoc/base/ordered_hash.rb', line 160
def __class__
OrderedHash
end
|
- (Object) class
157
158
159
|
# File 'lib/nanoc/base/ordered_hash.rb', line 157
def class
Hash
end
|
- (Object) clear
48
49
50
51
|
# File 'lib/nanoc/base/ordered_hash.rb', line 48
def clear
@order = []
super
end
|
- (Object) delete(key)
52
53
54
55
|
# File 'lib/nanoc/base/ordered_hash.rb', line 52
def delete key
@order.delete key
super
end
|
- (Object) delete_if
69
70
71
72
73
74
|
# File 'lib/nanoc/base/ordered_hash.rb', line 69
def delete_if
@order.clone.each { |k|
delete k if yield(k)
}
self
end
|
- (Object) each
Also known as:
each_pair
64
65
66
67
|
# File 'lib/nanoc/base/ordered_hash.rb', line 64
def each
@order.each { |k| yield k,self[k] }
self
end
|
- (Object) each_key
56
57
58
59
|
# File 'lib/nanoc/base/ordered_hash.rb', line 56
def each_key
@order.each { |k| yield k }
self
end
|
- (Object) each_value
60
61
62
63
|
# File 'lib/nanoc/base/ordered_hash.rb', line 60
def each_value
@order.each { |k| yield self[k] }
self
end
|
- (Object) each_with_index
192
193
194
195
|
# File 'lib/nanoc/base/ordered_hash.rb', line 192
def each_with_index
@order.each_with_index { |k, index| yield k, self[k], index }
self
end
|
- (Object) first
83
84
85
|
# File 'lib/nanoc/base/ordered_hash.rb', line 83
def first
{@order.first => self[@order.first]}
end
|
- (Object) inspect
139
140
141
142
143
|
# File 'lib/nanoc/base/ordered_hash.rb', line 139
def inspect
ary = []
each {|k,v| ary << k.inspect + "=>" + v.inspect}
'{' + ary.join(", ") + '}'
end
|
- (Object) invert
89
90
91
92
93
|
# File 'lib/nanoc/base/ordered_hash.rb', line 89
def invert
hsh2 = Hash.new
@order.each { |k| hsh2[self[k]] = k }
hsh2
end
|
- (Object) keys
80
81
82
|
# File 'lib/nanoc/base/ordered_hash.rb', line 80
def keys
@order
end
|
- (Object) last
86
87
88
|
# File 'lib/nanoc/base/ordered_hash.rb', line 86
def last
{@order.last => self[@order.last]}
end
|
- (Object) merge(hsh2)
149
150
151
|
# File 'lib/nanoc/base/ordered_hash.rb', line 149
def merge hsh2
self.dup update(hsh2)
end
|
- (Object) orig_store
38
|
# File 'lib/nanoc/base/ordered_hash.rb', line 38
alias orig_store store
|
- (Object) pop
127
128
129
130
|
# File 'lib/nanoc/base/ordered_hash.rb', line 127
def pop
key = @order.last
key ? [key,delete(key)] : nil
end
|
- (Object) push(k, v)
118
119
120
121
122
123
124
125
126
|
# File 'lib/nanoc/base/ordered_hash.rb', line 118
def push k,v
unless self.include? k
@order.push k
orig_store(k,v)
true
else
false
end
end
|
- (Object) reject(&block)
94
95
96
|
# File 'lib/nanoc/base/ordered_hash.rb', line 94
def reject &block
self.dup.delete_if &block
end
|
- (Object) reject!(&block)
97
98
99
100
|
# File 'lib/nanoc/base/ordered_hash.rb', line 97
def reject! &block
hsh2 = reject &block
self == hsh2 ? nil : hsh2
end
|
- (Object) replace(hsh2)
101
102
103
104
|
# File 'lib/nanoc/base/ordered_hash.rb', line 101
def replace hsh2
@order = hsh2.keys
super hsh2
end
|
- (Object) select
152
153
154
155
156
|
# File 'lib/nanoc/base/ordered_hash.rb', line 152
def select
ary = []
each { |k,v| ary << [k,v] if yield k,v }
ary
end
|
- (Object) shift
105
106
107
108
|
# File 'lib/nanoc/base/ordered_hash.rb', line 105
def shift
key = @order.first
key ? [key,delete(key)] : super
end
|
- (Object) store(a, b)
Also known as:
[]=
39
40
41
42
|
# File 'lib/nanoc/base/ordered_hash.rb', line 39
def store a,b
@order.push a unless has_key? a
super a,b
end
|
- (Object) store_only(a, b)
35
36
37
|
# File 'lib/nanoc/base/ordered_hash.rb', line 35
def store_only a,b
store a,b
end
|
- (Object) to_a
131
132
133
134
135
|
# File 'lib/nanoc/base/ordered_hash.rb', line 131
def to_a
ary = []
each { |k,v| ary << [k,v] }
ary
end
|
- (Object) to_s
136
137
138
|
# File 'lib/nanoc/base/ordered_hash.rb', line 136
def to_s
self.to_a.to_s
end
|
- (Object) unshift(k, v)
109
110
111
112
113
114
115
116
117
|
# File 'lib/nanoc/base/ordered_hash.rb', line 109
def unshift k,v
unless self.include? k
@order.unshift k
orig_store(k,v)
true
else
false
end
end
|
- (Object) update(hsh2)
Also known as:
merge!
144
145
146
147
|
# File 'lib/nanoc/base/ordered_hash.rb', line 144
def update hsh2
hsh2.each { |k,v| self[k] = v }
self
end
|
- (Object) values
75
76
77
78
79
|
# File 'lib/nanoc/base/ordered_hash.rb', line 75
def values
ary = []
@order.each { |k| ary.push self[k] }
ary
end
|
- (Object) yaml_inline!
190
|
# File 'lib/nanoc/base/ordered_hash.rb', line 190
def yaml_inline!() self.yaml_inline = true end
|
- (Object) yaml_inline=(bool)
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/nanoc/base/ordered_hash.rb', line 165
def yaml_inline= bool
if respond_to?("to_yaml_style")
self.to_yaml_style = :inline
else
unless defined? @__yaml_inline_meth
@__yaml_inline_meth =
lambda {|opts|
YAML::quick_emit(object_id, opts) {|emitter|
emitter << '{ ' << map{|kv| kv.join ': '}.join(', ') << ' }'
}
}
class << self
def to_yaml opts = {}
begin
@__yaml_inline ? @__yaml_inline_meth[ opts ] : super
rescue
@to_yaml_style = :inline
super
end
end
end
end
end
@__yaml_inline = bool
end
|