Class: OrderedHash

Inherits:
Hash
  • Object
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)

Methods included from Nanoc::HashExtensions

#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



33
34
35
36
# File 'lib/nanoc/base/ordered_hash.rb', line 33

def initialize(*a, &b)
  super
  @order = []
end

Instance Attribute Details

- (Object) order

Returns the value of attribute order



14
15
16
# File 'lib/nanoc/base/ordered_hash.rb', line 14

def order
  @order
end

- (Object) to_yaml_style

Returns the value of attribute to_yaml_style



195
196
197
# File 'lib/nanoc/base/ordered_hash.rb', line 195

def to_yaml_style
  @to_yaml_style
end

Class Method Details

+ (Object) [](*args)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nanoc/base/ordered_hash.rb', line 17

def [](*args)
  hsh = OrderedHash.new
  if args[0].is_a?(Hash)
    hsh.replace args[0]
  elsif args.size.odd?
    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) ==(other)



49
50
51
52
# File 'lib/nanoc/base/ordered_hash.rb', line 49

def ==(other)
  return false if @order != other.order
  super other
end

- (Object) __class__



191
192
193
# File 'lib/nanoc/base/ordered_hash.rb', line 191

def __class__
  OrderedHash
end

- (Object) class



187
188
189
# File 'lib/nanoc/base/ordered_hash.rb', line 187

def class
  Hash
end

- (Object) clear



54
55
56
57
# File 'lib/nanoc/base/ordered_hash.rb', line 54

def clear
  @order = []
  super
end

- (Object) delete(key)



59
60
61
62
# File 'lib/nanoc/base/ordered_hash.rb', line 59

def delete(key)
  @order.delete key
  super
end

- (Object) delete_if



80
81
82
83
84
85
# File 'lib/nanoc/base/ordered_hash.rb', line 80

def delete_if
  @order.clone.each do |k|
    delete k if yield(k)
  end
  self
end

- (Object) each Also known as: each_pair



74
75
76
77
# File 'lib/nanoc/base/ordered_hash.rb', line 74

def each
  @order.each { |k| yield k, self[k] }
  self
end

- (Object) each_key



64
65
66
67
# File 'lib/nanoc/base/ordered_hash.rb', line 64

def each_key
  @order.each { |k| yield k }
  self
end

- (Object) each_value



69
70
71
72
# File 'lib/nanoc/base/ordered_hash.rb', line 69

def each_value
  @order.each { |k| yield self[k] }
  self
end

- (Object) each_with_index



224
225
226
227
# File 'lib/nanoc/base/ordered_hash.rb', line 224

def each_with_index
  @order.each_with_index { |k, index| yield k, self[k], index }
  self
end

- (Object) first



97
98
99
# File 'lib/nanoc/base/ordered_hash.rb', line 97

def first
  { @order.first => self[@order.first] }
end

- (Object) inspect



165
166
167
168
169
# File 'lib/nanoc/base/ordered_hash.rb', line 165

def inspect
  ary = []
  each { |k, v| ary << k.inspect + '=>' + v.inspect }
  '{' + ary.join(', ') + '}'
end

- (Object) invert



105
106
107
108
109
# File 'lib/nanoc/base/ordered_hash.rb', line 105

def invert
  hsh2 = {}
  @order.each { |k| hsh2[self[k]] = k }
  hsh2
end

- (Object) keys



93
94
95
# File 'lib/nanoc/base/ordered_hash.rb', line 93

def keys
  @order
end

- (Object) last



101
102
103
# File 'lib/nanoc/base/ordered_hash.rb', line 101

def last
  { @order.last => self[@order.last] }
end

- (Object) merge(hsh2)



177
178
179
# File 'lib/nanoc/base/ordered_hash.rb', line 177

def merge(hsh2)
  dup update(hsh2)
end

- (Object) orig_store



41
# File 'lib/nanoc/base/ordered_hash.rb', line 41

alias_method :orig_store, :store

- (Object) pop



150
151
152
153
# File 'lib/nanoc/base/ordered_hash.rb', line 150

def pop
  key = @order.last
  key ? [key, delete(key)] : nil
end

- (Object) push(k, v)



140
141
142
143
144
145
146
147
148
# File 'lib/nanoc/base/ordered_hash.rb', line 140

def push(k, v)
  if self.include? k
    false
  else
    @order.push k
    orig_store(k, v)
    true
  end
end

- (Object) reject(&block)



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

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

- (Object) reject!(&block)



115
116
117
118
# File 'lib/nanoc/base/ordered_hash.rb', line 115

def reject!(&block)
  hsh2 = reject(&block)
  self == hsh2 ? nil : hsh2
end

- (Object) replace(hsh2)



120
121
122
123
# File 'lib/nanoc/base/ordered_hash.rb', line 120

def replace(hsh2)
  @order = hsh2.keys
  super hsh2
end

- (Object) select



181
182
183
184
185
# File 'lib/nanoc/base/ordered_hash.rb', line 181

def select
  ary = []
  each { |k, v| ary << [k, v] if yield k, v }
  ary
end

- (Object) shift



125
126
127
128
# File 'lib/nanoc/base/ordered_hash.rb', line 125

def shift
  key = @order.first
  key ? [key, delete(key)] : super
end

- (Object) store(a, b) Also known as: []=



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

def store(a, b)
  @order.push a unless key? a
  super a, b
end

- (Object) store_only(a, b)



38
39
40
# File 'lib/nanoc/base/ordered_hash.rb', line 38

def store_only(a, b)
  store a, b
end

- (Object) to_a



155
156
157
158
159
# File 'lib/nanoc/base/ordered_hash.rb', line 155

def to_a
  ary = []
  each { |k, v| ary << [k, v] }
  ary
end

- (Object) to_s



161
162
163
# File 'lib/nanoc/base/ordered_hash.rb', line 161

def to_s
  to_a.to_s
end

- (Object) unshift(k, v)



130
131
132
133
134
135
136
137
138
# File 'lib/nanoc/base/ordered_hash.rb', line 130

def unshift(k, v)
  if self.include? k
    false
  else
    @order.unshift k
    orig_store(k, v)
    true
  end
end

- (Object) update(hsh2) Also known as: merge!



171
172
173
174
# File 'lib/nanoc/base/ordered_hash.rb', line 171

def update(hsh2)
  hsh2.each { |k, v| self[k] = v }
  self
end

- (Object) values



87
88
89
90
91
# File 'lib/nanoc/base/ordered_hash.rb', line 87

def values
  ary = []
  @order.each { |k| ary.push self[k] }
  ary
end

- (Object) yaml_inline!



220
221
222
# File 'lib/nanoc/base/ordered_hash.rb', line 220

def yaml_inline!
  self.yaml_inline = true
end

- (Object) yaml_inline=(bool)



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/nanoc/base/ordered_hash.rb', line 196

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 do |opts|
        YAML.quick_emit(object_id, opts) do |emitter|
          emitter << '{ ' << map { |kv| kv.join ': ' }.join(', ') << ' }'
        end
      end
      class << self
        def to_yaml(opts = {})
          @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
        rescue
          @to_yaml_style = :inline
          super
        end
      end
    end
  end
  @__yaml_inline = bool
end