Class: OrderedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/rq/orderedhash.rb,
lib/rq/orderedautohash.rb

Overview

if false

Direct Known Subclasses

RQ::OrderedAutoHash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a, &b) ⇒ OrderedHash

–{{{



12
13
14
# File 'lib/rq/orderedautohash.rb', line 12

def initialize 
	@order = []
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



19
20
21
# File 'lib/rq/orderedhash.rb', line 19

def order
  @order
end

#to_yaml_styleObject

Returns the value of attribute to_yaml_style.



180
181
182
# File 'lib/rq/orderedhash.rb', line 180

def to_yaml_style
  @to_yaml_style
end

Class Method Details

.[](*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rq/orderedhash.rb', line 22

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



50
51
52
53
# File 'lib/rq/orderedhash.rb', line 50

def == hsh2
	return false if @order != hsh2.order
	super hsh2
end

#clearObject



55
56
57
58
# File 'lib/rq/orderedhash.rb', line 55

def clear
	@order = []
	super
end

#delete(key) ⇒ Object



60
61
62
63
# File 'lib/rq/orderedhash.rb', line 60

def delete key
	@order.delete key
	super
end

#delete_ifObject



81
82
83
84
85
86
# File 'lib/rq/orderedhash.rb', line 81

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

#eachObject Also known as: each_pair



75
76
77
78
# File 'lib/rq/orderedhash.rb', line 75

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

#each_keyObject



65
66
67
68
# File 'lib/rq/orderedhash.rb', line 65

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

#each_valueObject



70
71
72
73
# File 'lib/rq/orderedhash.rb', line 70

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

#inspectObject



158
159
160
161
162
# File 'lib/rq/orderedhash.rb', line 158

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

#invertObject



98
99
100
101
102
# File 'lib/rq/orderedhash.rb', line 98

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

#keysObject



94
95
96
# File 'lib/rq/orderedhash.rb', line 94

def keys
	@order
end

#merge(hsh2) ⇒ Object



170
171
172
# File 'lib/rq/orderedhash.rb', line 170

def merge hsh2
	self.dup update(hsh2)
end

#orig_storeObject



43
# File 'lib/rq/orderedhash.rb', line 43

alias orig_store store

#popObject



143
144
145
146
# File 'lib/rq/orderedhash.rb', line 143

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

#push(k, v) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/rq/orderedhash.rb', line 133

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

#reject(&block) ⇒ Object



104
105
106
# File 'lib/rq/orderedhash.rb', line 104

def reject &block
	self.dup.delete_if &block
end

#reject!(&block) ⇒ Object



108
109
110
111
# File 'lib/rq/orderedhash.rb', line 108

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

#replace(hsh2) ⇒ Object



113
114
115
116
# File 'lib/rq/orderedhash.rb', line 113

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

#selectObject



174
175
176
177
178
# File 'lib/rq/orderedhash.rb', line 174

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

#shiftObject



118
119
120
121
# File 'lib/rq/orderedhash.rb', line 118

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

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



44
45
46
47
# File 'lib/rq/orderedhash.rb', line 44

def store a,b
	@order.push a unless has_key? a
	super a,b
end

#store_only(a, b) ⇒ Object



39
40
41
# File 'lib/rq/orderedhash.rb', line 39

def store_only a,b
	store a,b
end

#to_aObject



148
149
150
151
152
# File 'lib/rq/orderedhash.rb', line 148

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

#to_sObject



154
155
156
# File 'lib/rq/orderedhash.rb', line 154

def to_s
	self.to_a.to_s
end

#unshift(k, v) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/rq/orderedhash.rb', line 123

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!



164
165
166
167
# File 'lib/rq/orderedhash.rb', line 164

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

#valuesObject



88
89
90
91
92
# File 'lib/rq/orderedhash.rb', line 88

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

#yaml_inline!Object



206
# File 'lib/rq/orderedhash.rb', line 206

def yaml_inline!() self.yaml_inline = true end

#yaml_inline=(bool) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rq/orderedhash.rb', line 181

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