ramaze 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +26 -6
- data/bin/ramaze +8 -1
- data/doc/AUTHORS +13 -11
- data/doc/CHANGELOG +472 -13
- data/doc/LEGAL +22 -0
- data/doc/README +1 -4
- data/doc/TODO +7 -2
- data/doc/changes.txt +472 -13
- data/doc/changes.xml +472 -13
- data/doc/meta/announcement.txt +21 -23
- data/doc/readme_chunks/appendix.txt +0 -3
- data/doc/readme_chunks/features.txt +1 -1
- data/examples/auth/auth.rb +49 -0
- data/examples/auth/template/layout.haml +20 -0
- data/examples/auth/template/login.haml +16 -0
- data/examples/blog/spec/blog.rb +1 -0
- data/examples/caching.rb +5 -6
- data/examples/css.rb +37 -0
- data/examples/layout.rb +3 -17
- data/examples/simple_auth.rb +23 -0
- data/examples/sourceview/public/images/file.gif +0 -0
- data/examples/sourceview/public/images/folder.gif +0 -0
- data/examples/sourceview/public/images/tv-collapsable-last.gif +0 -0
- data/examples/sourceview/public/images/tv-collapsable.gif +0 -0
- data/examples/sourceview/public/images/tv-expandable-last.gif +0 -0
- data/examples/sourceview/public/images/tv-expandable.gif +0 -0
- data/examples/sourceview/public/images/tv-item-last.gif +0 -0
- data/examples/sourceview/public/images/tv-item.gif +0 -0
- data/examples/sourceview/public/jquery.js +11 -0
- data/examples/sourceview/public/jquery.treeview.css +47 -0
- data/examples/sourceview/public/jquery.treeview.js +223 -0
- data/examples/sourceview/public/sourceview.js +16 -0
- data/examples/sourceview/sourceview.rb +74 -0
- data/examples/sourceview/template/index.haml +43 -0
- data/examples/templates/template/external.nag +28 -0
- data/examples/templates/template/external.xsl +57 -0
- data/examples/templates/template_amrita2.rb +2 -4
- data/examples/templates/template_erubis.rb +1 -1
- data/examples/templates/template_haml.rb +1 -1
- data/examples/templates/template_liquid.rb +2 -4
- data/examples/templates/template_markaby.rb +2 -4
- data/examples/templates/template_nagoro.rb +53 -0
- data/examples/templates/template_remarkably.rb +2 -4
- data/examples/templates/template_xslt.rb +52 -0
- data/examples/whywiki/spec/whywiki.rb +63 -0
- data/examples/whywiki/start.rb +11 -13
- data/examples/whywiki/template/edit.xhtml +3 -3
- data/examples/whywiki/template/show.xhtml +5 -8
- data/examples/wikore/spec/wikore.rb +115 -0
- data/examples/wikore/src/controller.rb +81 -0
- data/examples/wikore/src/model.rb +51 -0
- data/examples/wikore/start.rb +6 -0
- data/examples/wikore/template/index.xhtml +8 -0
- data/examples/wiktacular/spec/wiktacular.rb +1 -0
- data/examples/wiktacular/src/controller.rb +1 -1
- data/lib/ramaze.rb +6 -3
- data/lib/ramaze/action.rb +2 -13
- data/lib/ramaze/action/render.rb +36 -18
- data/lib/ramaze/adapter.rb +1 -1
- data/lib/ramaze/adapter/cgi.rb +0 -3
- data/lib/ramaze/adapter/evented_mongrel.rb +7 -0
- data/lib/ramaze/adapter/mongrel.rb +4 -13
- data/lib/ramaze/adapter/swiftiplied_mongrel.rb +7 -0
- data/lib/ramaze/cache.rb +12 -7
- data/lib/ramaze/contrib.rb +22 -0
- data/lib/ramaze/contrib/auto_params.rb +128 -0
- data/lib/ramaze/contrib/auto_params/get_args.rb +56 -0
- data/lib/ramaze/contrib/gzip_filter.rb +57 -0
- data/lib/ramaze/contrib/route.rb +40 -0
- data/lib/ramaze/contrib/sequel/fill.rb +12 -0
- data/lib/ramaze/controller.rb +17 -6
- data/lib/ramaze/controller/resolve.rb +51 -14
- data/lib/ramaze/dispatcher.rb +15 -13
- data/lib/ramaze/dispatcher/action.rb +4 -3
- data/lib/ramaze/dispatcher/directory.rb +3 -3
- data/lib/ramaze/dispatcher/error.rb +1 -1
- data/lib/ramaze/dispatcher/file.rb +17 -6
- data/lib/ramaze/error.rb +3 -0
- data/lib/ramaze/gestalt.rb +28 -8
- data/lib/ramaze/global.rb +26 -6
- data/lib/ramaze/global/globalstruct.rb +31 -6
- data/lib/ramaze/helper.rb +2 -1
- data/lib/ramaze/helper/aspect.rb +2 -2
- data/lib/ramaze/helper/auth.rb +6 -9
- data/lib/ramaze/helper/cache.rb +89 -9
- data/lib/ramaze/helper/cgi.rb +2 -2
- data/lib/ramaze/helper/formatting.rb +44 -0
- data/lib/ramaze/helper/link.rb +7 -5
- data/lib/ramaze/helper/partial.rb +6 -4
- data/lib/ramaze/helper/redirect.rb +24 -9
- data/lib/ramaze/helper/stack.rb +6 -1
- data/lib/ramaze/inform/growl.rb +1 -1
- data/lib/ramaze/inform/informer.rb +2 -1
- data/lib/ramaze/inform/informing.rb +6 -0
- data/lib/ramaze/inform/syslog.rb +1 -0
- data/lib/ramaze/snippets/array/put_within.rb +24 -0
- data/lib/ramaze/snippets/dictionary.rb +499 -0
- data/lib/ramaze/snippets/numeric/filesize_format.rb +19 -0
- data/lib/ramaze/snippets/ordered_set.rb +31 -0
- data/lib/ramaze/snippets/string/DIVIDE.rb +1 -1
- data/lib/ramaze/snippets/string/snake_case.rb +1 -1
- data/lib/ramaze/snippets/struct/values_at.rb +5 -1
- data/lib/ramaze/snippets/thread/into.rb +25 -0
- data/lib/ramaze/sourcereload.rb +39 -19
- data/lib/ramaze/spec/helper.rb +4 -1
- data/lib/ramaze/spec/helper/{context.rb → browser.rb} +3 -0
- data/lib/ramaze/spec/helper/layout.rb +3 -0
- data/lib/ramaze/spec/helper/minimal.rb +3 -0
- data/lib/ramaze/spec/helper/mock_http.rb +8 -5
- data/lib/ramaze/spec/helper/requester.rb +4 -1
- data/lib/ramaze/spec/helper/wrap.rb +15 -7
- data/lib/ramaze/template.rb +5 -1
- data/lib/ramaze/template/ezamar/engine.rb +2 -3
- data/lib/ramaze/template/ezamar/morpher.rb +26 -45
- data/lib/ramaze/template/nagoro.rb +53 -0
- data/lib/ramaze/template/none.rb +14 -0
- data/lib/ramaze/template/xslt.rb +96 -0
- data/lib/ramaze/tool.rb +0 -1
- data/lib/ramaze/tool/localize.rb +7 -2
- data/lib/ramaze/trinity/request.rb +1 -0
- data/lib/ramaze/trinity/session.rb +3 -3
- data/lib/ramaze/version.rb +2 -2
- data/rake_tasks/conf.rake +1 -2
- data/rake_tasks/{maintaince.rake → maintenance.rake} +25 -17
- data/spec/contrib/auto_params.rb +97 -0
- data/spec/contrib/route.rb +60 -0
- data/spec/contrib/sequel/fill.rb +46 -0
- data/spec/examples/caching.rb +1 -2
- data/spec/examples/css.rb +12 -0
- data/spec/examples/element.rb +0 -1
- data/spec/examples/hello.rb +0 -1
- data/spec/examples/simple.rb +0 -1
- data/spec/helper.rb +3 -2
- data/spec/ramaze/action/cache.rb +24 -7
- data/spec/ramaze/action/layout.rb +19 -11
- data/spec/ramaze/action/template/sub/sub_wrapper.xhtml +1 -0
- data/spec/ramaze/controller.rb +11 -0
- data/spec/ramaze/controller/template_resolving.rb +28 -27
- data/spec/ramaze/dispatcher.rb +32 -0
- data/spec/ramaze/dispatcher/directory.rb +2 -1
- data/spec/ramaze/element.rb +1 -1
- data/spec/ramaze/gestalt.rb +28 -0
- data/spec/ramaze/helper/aspect.rb +14 -3
- data/spec/ramaze/helper/cache.rb +78 -13
- data/spec/ramaze/helper/formatting.rb +20 -0
- data/spec/ramaze/helper/link.rb +2 -0
- data/spec/ramaze/helper/partial.rb +12 -1
- data/spec/ramaze/helper/redirect.rb +44 -8
- data/spec/ramaze/helper/stack.rb +3 -3
- data/spec/ramaze/helper/template/loop.xhtml +3 -0
- data/spec/ramaze/helper/template/num.xhtml +1 -0
- data/spec/ramaze/helper/template/recursive.xhtml +8 -0
- data/spec/ramaze/morpher.rb +25 -6
- data/spec/ramaze/params.rb +6 -2
- data/spec/ramaze/request.rb +5 -2
- data/spec/ramaze/session.rb +19 -0
- data/spec/ramaze/template.rb +2 -2
- data/spec/ramaze/template/amrita2.rb +2 -2
- data/spec/ramaze/template/erubis.rb +2 -2
- data/spec/ramaze/template/ezamar.rb +6 -3
- data/spec/ramaze/template/haml.rb +3 -3
- data/spec/ramaze/template/liquid.rb +1 -1
- data/spec/ramaze/template/markaby.rb +1 -1
- data/spec/ramaze/template/nagoro.rb +65 -0
- data/spec/ramaze/template/nagoro/another/long/action.nag +1 -0
- data/spec/ramaze/template/nagoro/combined.nag +1 -0
- data/spec/ramaze/template/nagoro/file_only.nag +1 -0
- data/spec/ramaze/template/nagoro/index.nag +1 -0
- data/spec/ramaze/template/nagoro/nested.nag +1 -0
- data/spec/ramaze/template/nagoro/some__long__action.nag +1 -0
- data/spec/ramaze/template/nagoro/sum.nag +1 -0
- data/spec/ramaze/template/remarkably.rb +1 -1
- data/spec/ramaze/template/sass.rb +1 -1
- data/spec/ramaze/template/xslt.rb +93 -0
- data/spec/ramaze/template/xslt/concat_words.xsl +16 -0
- data/spec/ramaze/template/xslt/index.xsl +14 -0
- data/spec/ramaze/template/xslt/products.xsl +32 -0
- data/spec/ramaze/template/xslt/ruby_version.xsl +14 -0
- data/spec/snippets/array/put_within.rb +32 -0
- data/spec/snippets/numeric/filesize_format.rb +12 -0
- data/spec/snippets/ordered_set.rb +56 -0
- data/spec/snippets/ramaze/caller_lines.rb +6 -3
- data/spec/snippets/string/snake_case.rb +3 -0
- metadata +118 -22
- data/doc/README.html +0 -737
- data/examples/fcgi.rb +0 -13
- data/lib/ramaze/snippets/numeric/human_readable_filesize_format.rb +0 -33
- data/lib/ramaze/tool/tidy.rb +0 -104
- data/spec/ramaze/controller/template/edit.xhtml +0 -1
- data/spec/ramaze/controller/template/edit/content.xhtml +0 -1
- data/spec/ramaze/tidy.rb +0 -12
- data/spec/snippets/numeric/human_readable_filesize_format.rb +0 -11
@@ -0,0 +1,499 @@
|
|
1
|
+
# = dictionary.rb
|
2
|
+
#
|
3
|
+
# == Copyright (c) 2005 Jan Molic, Thomas Sawyer
|
4
|
+
#
|
5
|
+
# Ruby License
|
6
|
+
#
|
7
|
+
# This module is free software. You may use, modify, and/or redistribute this
|
8
|
+
# software under the same terms as Ruby.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT
|
11
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
12
|
+
# FOR A PARTICULAR PURPOSE.
|
13
|
+
#
|
14
|
+
# == Special Thanks
|
15
|
+
#
|
16
|
+
# Ported from OrderHash 2.0, Copyright (c) 2005 jan molic
|
17
|
+
#
|
18
|
+
# Thanks to Andrew Johnson for his suggestions and fixes of Hash[],
|
19
|
+
# merge, to_a, inspect and shift.
|
20
|
+
#
|
21
|
+
# == Authors & Contributors
|
22
|
+
#
|
23
|
+
# * Jan Molic
|
24
|
+
# * Thomas Sawyer
|
25
|
+
|
26
|
+
# Author:: Jan Molic
|
27
|
+
# Copyright:: Copyright (c) 2006 Jan Molic
|
28
|
+
# License:: Ruby License
|
29
|
+
|
30
|
+
# = Dictionary
|
31
|
+
#
|
32
|
+
# The Dictionary class is a Hash that preserves order.
|
33
|
+
# So it has some array-like extensions also. By defualt
|
34
|
+
# a Dictionary object preserves insertion order, but any
|
35
|
+
# order can be specified including alphabetical key order.
|
36
|
+
#
|
37
|
+
# == Usage
|
38
|
+
#
|
39
|
+
# Just require this file and use Dictionary instead of Hash.
|
40
|
+
#
|
41
|
+
# # You can do simply
|
42
|
+
# hsh = Dictionary.new
|
43
|
+
# hsh['z'] = 1
|
44
|
+
# hsh['a'] = 2
|
45
|
+
# hsh['c'] = 3
|
46
|
+
# p hsh.keys #=> ['z','a','c']
|
47
|
+
#
|
48
|
+
# # or using Dictionary[] method
|
49
|
+
# hsh = Dictionary['z', 1, 'a', 2, 'c', 3]
|
50
|
+
# p hsh.keys #=> ['z','a','c']
|
51
|
+
#
|
52
|
+
# # but this don't preserve order
|
53
|
+
# hsh = Dictionary['z'=>1, 'a'=>2, 'c'=>3]
|
54
|
+
# p hsh.keys #=> ['a','c','z']
|
55
|
+
#
|
56
|
+
# # Dictionary has useful extensions: push, pop and unshift
|
57
|
+
# p hsh.push('to_end', 15) #=> true, key added
|
58
|
+
# p hsh.push('to_end', 30) #=> false, already - nothing happen
|
59
|
+
# p hsh.unshift('to_begin', 50) #=> true, key added
|
60
|
+
# p hsh.unshift('to_begin', 60) #=> false, already - nothing happen
|
61
|
+
# p hsh.keys #=> ["to_begin", "a", "c", "z", "to_end"]
|
62
|
+
# p hsh.pop #=> ["to_end", 15], if nothing remains, return nil
|
63
|
+
# p hsh.keys #=> ["to_begin", "a", "c", "z"]
|
64
|
+
# p hsh.shift #=> ["to_begin", 30], if nothing remains, return nil
|
65
|
+
#
|
66
|
+
# == Usage Notes
|
67
|
+
#
|
68
|
+
# * You can use #order_by to set internal sort order.
|
69
|
+
# * #<< takes a two element [k,v] array and inserts.
|
70
|
+
# * Use ::auto which creates Dictionay sub-entries as needed.
|
71
|
+
# * And ::alpha which creates a new Dictionary sorted by key.
|
72
|
+
|
73
|
+
module Ramaze
|
74
|
+
class Dictionary
|
75
|
+
|
76
|
+
class << self
|
77
|
+
|
78
|
+
#--
|
79
|
+
# TODO is this needed? Doesn't the super class do this?
|
80
|
+
#++
|
81
|
+
|
82
|
+
def []( *args )
|
83
|
+
hsh = new
|
84
|
+
if Hash === args[0]
|
85
|
+
hsh.replace(args[0])
|
86
|
+
elsif (args.size % 2) != 0
|
87
|
+
raise ArgumentError, "odd number of elements for Hash"
|
88
|
+
else
|
89
|
+
while !args.empty?
|
90
|
+
hsh[args.shift] = args.shift
|
91
|
+
end
|
92
|
+
end
|
93
|
+
hsh
|
94
|
+
end
|
95
|
+
|
96
|
+
# Like #new but the block sets the order.
|
97
|
+
#
|
98
|
+
def new_by( *args, &blk )
|
99
|
+
new(*args).order_by(&blk)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Alternate to #new which creates a dictionary sorted by key.
|
103
|
+
#
|
104
|
+
# d = Dictionary.alpha
|
105
|
+
# d["z"] = 1
|
106
|
+
# d["y"] = 2
|
107
|
+
# d["x"] = 3
|
108
|
+
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
109
|
+
#
|
110
|
+
# This is equivalent to:
|
111
|
+
#
|
112
|
+
# Dictionary.new.order_by { |key,value| key }
|
113
|
+
|
114
|
+
def alpha( *args, &block )
|
115
|
+
new( *args, &block ).order_by_key
|
116
|
+
end
|
117
|
+
|
118
|
+
# Alternate to #new which auto-creates sub-dictionaries as needed.
|
119
|
+
#
|
120
|
+
# d = Dictionary.auto
|
121
|
+
# d["a"]["b"]["c"] = "abc" #=> { "a"=>{"b"=>{"c"=>"abc"}}}
|
122
|
+
#
|
123
|
+
def auto( *args )
|
124
|
+
#AutoDictionary.new(*args)
|
125
|
+
leet = lambda { |hsh, key| hsh[key] = new( &leet ) }
|
126
|
+
new(*args, &leet)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def initialize( *args, &blk )
|
131
|
+
@order = []
|
132
|
+
@order_by = nil
|
133
|
+
@hash = Hash.new( *args, &blk )
|
134
|
+
end
|
135
|
+
|
136
|
+
def order
|
137
|
+
reorder if @order_by
|
138
|
+
@order
|
139
|
+
end
|
140
|
+
|
141
|
+
# Keep dictionary sorted by a specific sort order.
|
142
|
+
|
143
|
+
def order_by( &block )
|
144
|
+
@order_by = block
|
145
|
+
order
|
146
|
+
self
|
147
|
+
end
|
148
|
+
|
149
|
+
# Keep dictionary sorted by key.
|
150
|
+
#
|
151
|
+
# d = Dictionary.new.order_by_key
|
152
|
+
# d["z"] = 1
|
153
|
+
# d["y"] = 2
|
154
|
+
# d["x"] = 3
|
155
|
+
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
156
|
+
#
|
157
|
+
# This is equivalent to:
|
158
|
+
#
|
159
|
+
# Dictionary.new.order_by { |key,value| key }
|
160
|
+
#
|
161
|
+
# The initializer Dictionary#alpha also provides this.
|
162
|
+
|
163
|
+
def order_by_key
|
164
|
+
@order_by = lambda { |k,v| k }
|
165
|
+
order
|
166
|
+
self
|
167
|
+
end
|
168
|
+
|
169
|
+
# Keep dictionary sorted by value.
|
170
|
+
#
|
171
|
+
# d = Dictionary.new.order_by_value
|
172
|
+
# d["z"] = 1
|
173
|
+
# d["y"] = 2
|
174
|
+
# d["x"] = 3
|
175
|
+
# d #=> {"x"=>3,"y"=>2,"z"=>2}
|
176
|
+
#
|
177
|
+
# This is equivalent to:
|
178
|
+
#
|
179
|
+
# Dictionary.new.order_by { |key,value| value }
|
180
|
+
|
181
|
+
def order_by_value
|
182
|
+
@order_by = lambda { |k,v| v }
|
183
|
+
order
|
184
|
+
self
|
185
|
+
end
|
186
|
+
|
187
|
+
#
|
188
|
+
|
189
|
+
def reorder
|
190
|
+
if @order_by
|
191
|
+
assoc = @order.collect{ |k| [k,@hash[k]] }.sort_by( &@order_by )
|
192
|
+
@order = assoc.collect{ |k,v| k }
|
193
|
+
end
|
194
|
+
@order
|
195
|
+
end
|
196
|
+
|
197
|
+
#def ==( hsh2 )
|
198
|
+
# return false if @order != hsh2.order
|
199
|
+
# super hsh2
|
200
|
+
#end
|
201
|
+
|
202
|
+
def ==( hsh2 )
|
203
|
+
if hsh2.is_a?( Dictionary )
|
204
|
+
@order == hsh2.order &&
|
205
|
+
@hash == hsh2.instance_variable_get("@hash")
|
206
|
+
else
|
207
|
+
false
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
def [] k
|
212
|
+
@hash[ k ]
|
213
|
+
end
|
214
|
+
|
215
|
+
def fetch( k )
|
216
|
+
@hash.fetch( k )
|
217
|
+
end
|
218
|
+
|
219
|
+
# Store operator.
|
220
|
+
#
|
221
|
+
# h[key] = value
|
222
|
+
#
|
223
|
+
# Or with additional index.
|
224
|
+
#
|
225
|
+
# h[key,index] = value
|
226
|
+
|
227
|
+
def []=(k, i=nil, v=nil)
|
228
|
+
if v
|
229
|
+
insert(i,k,v)
|
230
|
+
else
|
231
|
+
store(k,i)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
def insert( i,k,v )
|
236
|
+
@order.insert( i,k )
|
237
|
+
@hash.store( k,v )
|
238
|
+
end
|
239
|
+
|
240
|
+
def store( a,b )
|
241
|
+
@order.push( a ) unless @hash.has_key?( a )
|
242
|
+
@hash.store( a,b )
|
243
|
+
end
|
244
|
+
|
245
|
+
def clear
|
246
|
+
@order = []
|
247
|
+
@hash.clear
|
248
|
+
end
|
249
|
+
|
250
|
+
def delete( key )
|
251
|
+
@order.delete( key )
|
252
|
+
@hash.delete( key )
|
253
|
+
end
|
254
|
+
|
255
|
+
def each_key
|
256
|
+
order.each { |k| yield( k ) }
|
257
|
+
self
|
258
|
+
end
|
259
|
+
|
260
|
+
def each_value
|
261
|
+
order.each { |k| yield( @hash[k] ) }
|
262
|
+
self
|
263
|
+
end
|
264
|
+
|
265
|
+
def each
|
266
|
+
order.each { |k| yield( k,@hash[k] ) }
|
267
|
+
self
|
268
|
+
end
|
269
|
+
alias each_pair each
|
270
|
+
|
271
|
+
def delete_if
|
272
|
+
order.clone.each { |k| delete k if yield }
|
273
|
+
self
|
274
|
+
end
|
275
|
+
|
276
|
+
def values
|
277
|
+
ary = []
|
278
|
+
order.each { |k| ary.push @hash[k] }
|
279
|
+
ary
|
280
|
+
end
|
281
|
+
|
282
|
+
def keys
|
283
|
+
order
|
284
|
+
end
|
285
|
+
|
286
|
+
def invert
|
287
|
+
hsh2 = self.class.new
|
288
|
+
order.each { |k| hsh2[@hash[k]] = k }
|
289
|
+
hsh2
|
290
|
+
end
|
291
|
+
|
292
|
+
def reject( &block )
|
293
|
+
self.dup.delete_if &block
|
294
|
+
end
|
295
|
+
|
296
|
+
def reject!( &block )
|
297
|
+
hsh2 = reject &block
|
298
|
+
self == hsh2 ? nil : hsh2
|
299
|
+
end
|
300
|
+
|
301
|
+
def replace( hsh2 )
|
302
|
+
@order = hsh2.order
|
303
|
+
@hash = hsh2.hash
|
304
|
+
end
|
305
|
+
|
306
|
+
def shift
|
307
|
+
key = order.first
|
308
|
+
key ? [key,delete(key)] : super
|
309
|
+
end
|
310
|
+
|
311
|
+
def unshift( k,v )
|
312
|
+
unless @hash.include?( k )
|
313
|
+
@order.unshift( k )
|
314
|
+
@hash.store( k,v )
|
315
|
+
true
|
316
|
+
else
|
317
|
+
false
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
def <<(kv)
|
322
|
+
push *kv
|
323
|
+
end
|
324
|
+
|
325
|
+
def push( k,v )
|
326
|
+
unless @hash.include?( k )
|
327
|
+
@order.push( k )
|
328
|
+
@hash.store( k,v )
|
329
|
+
true
|
330
|
+
else
|
331
|
+
false
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
def pop
|
336
|
+
key = order.last
|
337
|
+
key ? [key,delete(key)] : nil
|
338
|
+
end
|
339
|
+
|
340
|
+
def to_a
|
341
|
+
ary = []
|
342
|
+
each { |k,v| ary << [k,v] }
|
343
|
+
ary
|
344
|
+
end
|
345
|
+
|
346
|
+
def to_s
|
347
|
+
self.to_a.to_s
|
348
|
+
end
|
349
|
+
|
350
|
+
def inspect
|
351
|
+
ary = []
|
352
|
+
each {|k,v| ary << k.inspect + "=>" + v.inspect}
|
353
|
+
'{' + ary.join(", ") + '}'
|
354
|
+
end
|
355
|
+
|
356
|
+
def dup
|
357
|
+
self.class[*to_a.flatten]
|
358
|
+
end
|
359
|
+
|
360
|
+
def update( hsh2 )
|
361
|
+
hsh2.each { |k,v| self[k] = v }
|
362
|
+
reorder
|
363
|
+
self
|
364
|
+
end
|
365
|
+
alias :merge! update
|
366
|
+
|
367
|
+
def merge( hsh2 )
|
368
|
+
self.dup.update(hsh2)
|
369
|
+
end
|
370
|
+
|
371
|
+
def select
|
372
|
+
ary = []
|
373
|
+
each { |k,v| ary << [k,v] if yield k,v }
|
374
|
+
ary
|
375
|
+
end
|
376
|
+
|
377
|
+
def first
|
378
|
+
@hash[order.first]
|
379
|
+
end
|
380
|
+
|
381
|
+
def last
|
382
|
+
@hash[order.last]
|
383
|
+
end
|
384
|
+
|
385
|
+
def length
|
386
|
+
@order.length
|
387
|
+
end
|
388
|
+
alias :size :length
|
389
|
+
|
390
|
+
def empty?
|
391
|
+
@hash.empty?
|
392
|
+
end
|
393
|
+
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
# _____ _
|
400
|
+
# |_ _|__ ___| |_
|
401
|
+
# | |/ _ \/ __| __|
|
402
|
+
# | | __/\__ \ |_
|
403
|
+
# |_|\___||___/\__|
|
404
|
+
#
|
405
|
+
|
406
|
+
=begin testing
|
407
|
+
|
408
|
+
require 'test/unit'
|
409
|
+
|
410
|
+
class TC_Dictionary < Test::Unit::TestCase
|
411
|
+
|
412
|
+
def test_create
|
413
|
+
hsh = Dictionary['z', 1, 'a', 2, 'c', 3]
|
414
|
+
assert_equal( ['z','a','c'], hsh.keys )
|
415
|
+
end
|
416
|
+
|
417
|
+
def test_op_store
|
418
|
+
hsh = Dictionary.new
|
419
|
+
hsh['z'] = 1
|
420
|
+
hsh['a'] = 2
|
421
|
+
hsh['c'] = 3
|
422
|
+
assert_equal( ['z','a','c'], hsh.keys )
|
423
|
+
end
|
424
|
+
|
425
|
+
def test_push
|
426
|
+
hsh = Dictionary['a', 1, 'c', 2, 'z', 3]
|
427
|
+
assert( hsh.push('end', 15) )
|
428
|
+
assert_equal( 15, hsh['end'] )
|
429
|
+
assert( ! hsh.push('end', 30) )
|
430
|
+
assert( hsh.unshift('begin', 50) )
|
431
|
+
assert_equal( 50, hsh['begin'] )
|
432
|
+
assert( ! hsh.unshift('begin', 60) )
|
433
|
+
assert_equal( ["begin", "a", "c", "z", "end"], hsh.keys )
|
434
|
+
assert_equal( ["end", 15], hsh.pop )
|
435
|
+
assert_equal( ["begin", "a", "c", "z"], hsh.keys )
|
436
|
+
assert_equal( ["begin", 50], hsh.shift )
|
437
|
+
end
|
438
|
+
|
439
|
+
def test_insert
|
440
|
+
# front
|
441
|
+
h = Dictionary['a', 1, 'b', 2, 'c', 3]
|
442
|
+
r = Dictionary['d', 4, 'a', 1, 'b', 2, 'c', 3]
|
443
|
+
assert_equal( 4, h.insert(0,'d',4) )
|
444
|
+
assert_equal( r, h )
|
445
|
+
# back
|
446
|
+
h = Dictionary['a', 1, 'b', 2, 'c', 3]
|
447
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
448
|
+
assert_equal( 4, h.insert(-1,'d',4) )
|
449
|
+
assert_equal( r, h )
|
450
|
+
end
|
451
|
+
|
452
|
+
def test_update
|
453
|
+
# with other orderred hash
|
454
|
+
h1 = Dictionary['a', 1, 'b', 2, 'c', 3]
|
455
|
+
h2 = Dictionary['d', 4]
|
456
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
457
|
+
assert_equal( r, h1.update(h2) )
|
458
|
+
assert_equal( r, h1 )
|
459
|
+
# with other hash
|
460
|
+
h1 = Dictionary['a', 1, 'b', 2, 'c', 3]
|
461
|
+
h2 = { 'd' => 4 }
|
462
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
463
|
+
assert_equal( r, h1.update(h2) )
|
464
|
+
assert_equal( r, h1 )
|
465
|
+
end
|
466
|
+
|
467
|
+
def test_merge
|
468
|
+
# with other orderred hash
|
469
|
+
h1 = Dictionary['a', 1, 'b', 2, 'c', 3]
|
470
|
+
h2 = Dictionary['d', 4]
|
471
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
472
|
+
assert_equal( r, h1.merge(h2) )
|
473
|
+
# with other hash
|
474
|
+
h1 = Dictionary['a', 1, 'b', 2, 'c', 3]
|
475
|
+
h2 = { 'd' => 4 }
|
476
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
477
|
+
assert_equal( r, h1.merge(h2) )
|
478
|
+
end
|
479
|
+
|
480
|
+
def test_order_by
|
481
|
+
h1 = Dictionary['a', 3, 'b', 2, 'c', 1]
|
482
|
+
h1.order_by{ |k,v| v }
|
483
|
+
assert_equal( [1,2,3], h1.values )
|
484
|
+
assert_equal( ['c','b','a'], h1.keys )
|
485
|
+
end
|
486
|
+
|
487
|
+
def test_op_store
|
488
|
+
h1 = Dictionary[]
|
489
|
+
h1[:a] = 1
|
490
|
+
h1[:c] = 3
|
491
|
+
assert_equal( [1,3], h1.values )
|
492
|
+
h1[:b,1] = 2
|
493
|
+
assert_equal( [1,2,3], h1.values )
|
494
|
+
assert_equal( [:a,:b,:c], h1.keys )
|
495
|
+
end
|
496
|
+
|
497
|
+
end
|
498
|
+
|
499
|
+
=end
|