Class: ALib::OrderedHash

Inherits:
Hash show all
Defined in:
lib/alib-0.5.1/orderedhash.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

Direct Known Subclasses

AutoOrderedHash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#getopt, #hasopt?

Constructor Details

#initialize(*a, &b) ⇒ OrderedHash

end



37
38
39
40
41
42
# File 'lib/alib-0.5.1/orderedhash.rb', line 37

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

Instance Attribute Details

#orderObject

–{{{



13
14
15
# File 'lib/alib-0.5.1/orderedhash.rb', line 13

def order
  @order
end

#to_yaml_styleObject

Returns the value of attribute to_yaml_style.



215
216
217
# File 'lib/alib-0.5.1/orderedhash.rb', line 215

def to_yaml_style
  @to_yaml_style
end

Class Method Details

.[](*args) ⇒ Object

–{{{



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/alib-0.5.1/orderedhash.rb', line 17

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
        hsh[args.shift] = args.shift while args.size > 0
    end
    hsh
#--}}}
end

Instance Method Details

#==(hsh2) ⇒ Object



56
57
58
59
60
61
# File 'lib/alib-0.5.1/orderedhash.rb', line 56

def == hsh2
#--{{{
    return false if @order != hsh2.order
    super hsh2
#--}}}
end

#classObject

–}}}



209
210
211
212
213
# File 'lib/alib-0.5.1/orderedhash.rb', line 209

def class
#--{{{
  Hash
#--}}}
end

#clearObject

–}}}



62
63
64
65
66
67
# File 'lib/alib-0.5.1/orderedhash.rb', line 62

def clear
#--{{{
    @order = []
    super
#--}}}
end

#delete(key) ⇒ Object

–}}}



68
69
70
71
72
73
# File 'lib/alib-0.5.1/orderedhash.rb', line 68

def delete key
#--{{{
    @order.delete key
    super
#--}}}
end

#delete_ifObject



93
94
95
96
97
98
99
100
# File 'lib/alib-0.5.1/orderedhash.rb', line 93

def delete_if
#--{{{
    @order.clone.each { |k| 
        delete k if yield
    }
    self
#--}}}
end

#eachObject Also known as: each_pair

–}}}



86
87
88
89
90
91
# File 'lib/alib-0.5.1/orderedhash.rb', line 86

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

#each_keyObject

–}}}



74
75
76
77
78
79
# File 'lib/alib-0.5.1/orderedhash.rb', line 74

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

#each_valueObject

–}}}



80
81
82
83
84
85
# File 'lib/alib-0.5.1/orderedhash.rb', line 80

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

#inspectObject

–}}}



183
184
185
186
187
188
189
# File 'lib/alib-0.5.1/orderedhash.rb', line 183

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

#invertObject

–}}}



113
114
115
116
117
118
119
# File 'lib/alib-0.5.1/orderedhash.rb', line 113

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

#keysObject

–}}}



108
109
110
111
112
# File 'lib/alib-0.5.1/orderedhash.rb', line 108

def keys
#--{{{
    @order
#--}}}
end

#merge(hsh2) ⇒ Object



197
198
199
200
201
# File 'lib/alib-0.5.1/orderedhash.rb', line 197

def merge hsh2
#--{{{
    self.dup update(hsh2)
#--}}}
end

#orig_storeObject

–}}}



48
# File 'lib/alib-0.5.1/orderedhash.rb', line 48

alias orig_store store

#popObject

–}}}



165
166
167
168
169
170
# File 'lib/alib-0.5.1/orderedhash.rb', line 165

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

#push(k, v) ⇒ Object

–}}}



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/alib-0.5.1/orderedhash.rb', line 154

def push k,v
#--{{{
    unless self.include? k
        @order.push k
        orig_store(k,v)
        true
    else
        false
    end
#--}}}
end

#reject(&block) ⇒ Object

–}}}



120
121
122
123
124
# File 'lib/alib-0.5.1/orderedhash.rb', line 120

def reject &block
#--{{{
    self.dup.delete_if &block
#--}}}
end

#reject!(&block) ⇒ Object

–}}}



125
126
127
128
129
130
# File 'lib/alib-0.5.1/orderedhash.rb', line 125

def reject! &block
#--{{{
    hsh2 = reject &block
    self == hsh2 ? nil : hsh2
#--}}}
end

#replace(hsh2) ⇒ Object

–}}}



131
132
133
134
135
136
# File 'lib/alib-0.5.1/orderedhash.rb', line 131

def replace hsh2
#--{{{
    @order = hsh2.keys 
    super hsh2
#--}}}
end

#selectObject

–}}}



202
203
204
205
206
207
208
# File 'lib/alib-0.5.1/orderedhash.rb', line 202

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

#shiftObject

–}}}



137
138
139
140
141
142
# File 'lib/alib-0.5.1/orderedhash.rb', line 137

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

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



49
50
51
52
53
54
# File 'lib/alib-0.5.1/orderedhash.rb', line 49

def store a,b
#--{{{
    @order.push a unless has_key? a
    super a,b
#--}}}
end

#store_only(a, b) ⇒ Object

–}}}



43
44
45
46
47
# File 'lib/alib-0.5.1/orderedhash.rb', line 43

def store_only a,b
#--{{{
    store a,b
#--}}}
end

#to_aObject

–}}}



171
172
173
174
175
176
177
# File 'lib/alib-0.5.1/orderedhash.rb', line 171

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

#to_sObject

–}}}



178
179
180
181
182
# File 'lib/alib-0.5.1/orderedhash.rb', line 178

def to_s
#--{{{
    self.to_a.to_s
#--}}}
end

#unshift(k, v) ⇒ Object

–}}}



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/alib-0.5.1/orderedhash.rb', line 143

def unshift k,v
#--{{{
    unless self.include? k
        @order.unshift k
        orig_store(k,v)
        true
    else
        false
    end
#--}}}
end

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

–}}}



190
191
192
193
194
195
# File 'lib/alib-0.5.1/orderedhash.rb', line 190

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

#valuesObject

–}}}



101
102
103
104
105
106
107
# File 'lib/alib-0.5.1/orderedhash.rb', line 101

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

#yaml_inline!Object



241
# File 'lib/alib-0.5.1/orderedhash.rb', line 241

def yaml_inline!() self.yaml_inline = true end

#yaml_inline=(bool) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/alib-0.5.1/orderedhash.rb', line 216

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