ruby-path 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ === 1.0.1 / 2012-07-18
2
+
3
+ * Bugfixes:
4
+
5
+ * Update homepage
6
+
1
7
  === 1.0.0 / 2012-03-27
2
8
 
3
9
  * Major Enhancements:
@@ -12,3 +12,4 @@ test/test_path.rb
12
12
  test/test_core_ext.rb
13
13
  test/test_path_match.rb
14
14
  test/test_path_matcher.rb
15
+ test/test_path_transaction.rb
@@ -1,6 +1,6 @@
1
1
  = ruby-path
2
2
 
3
- * https://github.com/yaksnrainbows/ruby-path
3
+ * https://yaks.me/ruby-path
4
4
 
5
5
  == DESCRIPTION:
6
6
 
@@ -42,8 +42,8 @@ Simple path-based search and lookup for Ruby.
42
42
  === Delete Data:
43
43
 
44
44
  data = {:foo => "bar", :foobar => [:a, :b, {:foo => "other bar"}, :c]}
45
- data.replace_at_path "**=*bar", "BAR"
46
- #=> {[:foo] => "bar", [:foobar, 2, :foo] => "other bar"}
45
+ data.delete_at_path "**=*bar"
46
+ #=> {:foobar => [:a, :b, {}, :c]}
47
47
 
48
48
  === Existance of Data:
49
49
 
@@ -32,7 +32,7 @@
32
32
  # Path.find "root/**=\\1..3", data
33
33
 
34
34
  class Path
35
- VERSION = '1.0.0'
35
+ VERSION = '1.0.1'
36
36
 
37
37
 
38
38
  # Used as path instruction to go up one path level.
@@ -82,8 +82,8 @@ module Path::DataExt
82
82
  # Returns a hash of path/value pairs of deleted items.
83
83
  #
84
84
  # data = {:foo => "bar", :foobar => [:a, :b, {:foo => "other bar"}, :c]}
85
- # data.replace_at_path "**=*bar", "BAR"
86
- # #=> {[:foo] => "bar", [:foobar, 2, :foo] => "other bar"}
85
+ # data.delete_at_path "**=*bar"
86
+ # #=> {:foobar => [:a, :b, {}, :c]}
87
87
 
88
88
  def delete_at_path path, limit=nil
89
89
  count = 0
@@ -0,0 +1,520 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestTransaction < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @data = {
7
+ :key1 => {
8
+ :key1a => [
9
+ "foo",
10
+ "bar",
11
+ "foobar",
12
+ {:findme => "thing"}
13
+ ],
14
+ 'key1b' => "findme"
15
+ },
16
+ 'findme' => [
17
+ 123,
18
+ 456,
19
+ {:findme => 123456}
20
+ ],
21
+ :key2 => "foobar",
22
+ :key3 => {
23
+ :key3a => ["val1", "val2", "val3"]
24
+ }
25
+ }
26
+
27
+ @trans = Path::Transaction.new @data
28
+ end
29
+
30
+
31
+ def test_many_transactions
32
+ data = @trans.run do |t|
33
+ t.select "findme/0..1", "key(1|3)"
34
+ t.move [["key3/*/0", "key_value"], ["key?", "thing%1"]]
35
+ t.delete "findme/0"
36
+ t.move "findme/1" => "last_thing"
37
+ end
38
+
39
+ expected = {
40
+ "last_thing"=>456 , "key_value"=>"val1", "findme" => [],
41
+ "thing1"=>{:key1a=>["foo", "bar", "foobar", {:findme=>"thing"}],
42
+ "key1b"=>"findme"},
43
+ "thing3"=>{:key3a=>["val2", "val3"]}}
44
+
45
+ assert_equal expected, data
46
+ end
47
+
48
+
49
+ def test_many_transactions_with_map
50
+ data = @trans.run do |t|
51
+ t.select "findme/0..1", "key(1|3)"
52
+ t.map [["key3/*/0", "key_value"], ["key?", "thing%1"]]
53
+ t.delete "findme/0"
54
+ t.move "findme/1" => "last_thing"
55
+ end
56
+
57
+ expected = {
58
+ "key_value"=>"val1",
59
+ "thing1"=>{:key1a=>["foo", "bar", "foobar", {:findme=>"thing"}],
60
+ "key1b"=>"findme"},
61
+ "thing3"=>{:key3a=>["val2", "val3"]}}
62
+
63
+ assert_equal expected, data
64
+ end
65
+
66
+
67
+ def test_class_run
68
+ block = lambda do |t|
69
+ t.delete "key3/key*/2", "**=thing"
70
+ t.select "**=foo*", "**/findme"
71
+ end
72
+
73
+ assert_equal @trans.run(&block),
74
+ Path::Transaction.run(@data, &block)
75
+
76
+ assert_equal @trans.run(:keep_indicies => true, &block),
77
+ Path::Transaction.run(@data, :keep_indicies => true, &block)
78
+ end
79
+
80
+
81
+ def test_run
82
+ expected = {
83
+ :key1=>{:key1a=>["foo", "foobar"]},
84
+ :key2=>"foobar",
85
+ "findme"=>[123, 456, {:findme=>123456}]
86
+ }
87
+
88
+ result = @trans.run do |t|
89
+ t.delete "key3/key*/2", "**=thing"
90
+ t.select "**=foo*", "**/findme"
91
+ end
92
+
93
+ assert_equal expected, result
94
+ assert_equal @data, @trans.run
95
+
96
+ result2 = @trans.run do |t|
97
+ t.delete "key3/key*/2", "**=thing"
98
+ t.select "**=foo*", "**/findme"
99
+ end
100
+
101
+ assert_equal expected, result2
102
+ end
103
+
104
+
105
+ def test_results
106
+ @trans.clear
107
+ @trans.delete "key3/key*/2", "**=thing"
108
+ @trans.select "**=foo*", "**/findme"
109
+ result = @trans.results
110
+
111
+ expected = {
112
+ :key1=>{:key1a=>["foo", "foobar"]},
113
+ :key2=>"foobar",
114
+ "findme"=>[123, 456, {:findme=>123456}]
115
+ }
116
+
117
+ assert_equal expected, result
118
+ end
119
+
120
+
121
+ def test_results_keep_indicies
122
+ @trans.clear
123
+ @trans.delete "key3/key*/2", "**=thing"
124
+ @trans.select "**=foo*", "**/findme"
125
+ result = @trans.results :keep_indicies => true
126
+
127
+ expected = {
128
+ :key1=>{:key1a=>{2=>"foobar", 0=>"foo"}},
129
+ :key2=>"foobar",
130
+ "findme"=>[123, 456, {:findme=>123456}]
131
+ }
132
+
133
+ assert_equal expected, result
134
+ end
135
+
136
+
137
+ def test_remake_arrays_select
138
+ result = @trans.transaction_select @data, "**=foo", "key3/key*/2"
139
+ result = @trans.remake_arrays result
140
+
141
+ expected = {:key1=>{:key1a=>["foo"]}, :key3=>{:key3a=>["val3"]}}
142
+
143
+ assert_equal expected, result
144
+ end
145
+
146
+
147
+ def test_remake_arrays_select_except_modified
148
+ result = @trans.transaction_select @data, "**=foo", "key3/key*/2"
149
+ result = @trans.remake_arrays result, true
150
+
151
+ expected = {:key1=>{:key1a=>{0=>"foo"}}, :key3=>{:key3a=>{2=>"val3"}}}
152
+
153
+ assert_equal expected, result
154
+ end
155
+
156
+
157
+ def test_remake_arrays_select_root
158
+ data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
159
+
160
+ @trans = Path::Transaction.new data_arr
161
+ result = @trans.transaction_select data_arr, "**=foo", "3/key*/2"
162
+ result = @trans.remake_arrays result
163
+
164
+ expected = [{:key1a=>["foo"]}, {:key3a=>["val3"]}]
165
+
166
+ assert_equal expected, result
167
+ end
168
+
169
+
170
+ def test_remake_arrays_select_root_except_modified
171
+ data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
172
+
173
+ @trans = Path::Transaction.new data_arr
174
+ result = @trans.transaction_select data_arr, "**=foo", "3/key*"
175
+ result = @trans.remake_arrays result, true
176
+
177
+ expected = {1=>{:key1a=>{0=>"foo"}}, 3=>{:key3a=>["val1", "val2", "val3"]}}
178
+
179
+ assert_equal expected, result
180
+ end
181
+
182
+
183
+ def test_remake_arrays_delete
184
+ result = @trans.transaction_delete @data, "**=foo", "key3/key*/2"
185
+ result = @trans.remake_arrays result
186
+
187
+ expected = {
188
+ :key1 => {
189
+ :key1a => ["bar", "foobar", {:findme => "thing"}],
190
+ 'key1b' => "findme"
191
+ },
192
+ 'findme' => [123, 456, {:findme => 123456}],
193
+ :key2 => "foobar",
194
+ :key3 => {
195
+ :key3a => ["val1", "val2"]
196
+ }
197
+ }
198
+
199
+ assert_equal expected, result
200
+ end
201
+
202
+
203
+ def test_remake_arrays_delete_except_modified
204
+ result = @trans.transaction_delete @data, "**=foo", "key3/key*/2"
205
+ result = @trans.remake_arrays result, true
206
+
207
+ expected = {
208
+ :key1 => {
209
+ :key1a => {1=>"bar", 2=>"foobar", 3=>{:findme=>"thing"}},
210
+ 'key1b' => "findme"
211
+ },
212
+ 'findme' => [123, 456, {:findme => 123456}],
213
+ :key2 => "foobar",
214
+ :key3 => {
215
+ :key3a => {0=>"val1", 1=>"val2"}
216
+ }
217
+ }
218
+
219
+ assert_equal expected, result
220
+ end
221
+
222
+
223
+ def test_remake_arrays_delete_root
224
+ data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
225
+
226
+ @trans = Path::Transaction.new data_arr
227
+ result = @trans.transaction_delete data_arr, "**=foo", "3/key*/2"
228
+ result = @trans.remake_arrays result
229
+
230
+ expected = [
231
+ [123, 456, {:findme=>123456}],
232
+ {:key1a=>["bar", "foobar", {:findme=>"thing"}], "key1b"=>"findme"},
233
+ "foobar",
234
+ {:key3a=>["val1", "val2"]}
235
+ ]
236
+
237
+ assert_equal expected, result
238
+ end
239
+
240
+
241
+ def test_remake_arrays_delete_root_except_modified
242
+ data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
243
+
244
+ @trans = Path::Transaction.new data_arr
245
+ result = @trans.transaction_delete data_arr, "**=foo", "3/key*/2"
246
+ result = @trans.remake_arrays result, true
247
+
248
+ expected = [
249
+ [123, 456, {:findme=>123456}],
250
+ {:key1a=>{1=>"bar", 2=>"foobar", 3=>{:findme=>"thing"}},
251
+ "key1b"=>"findme"},
252
+ "foobar",
253
+ {:key3a=>{0=>"val1", 1=>"val2"}}
254
+ ]
255
+
256
+ assert_equal expected, result
257
+ end
258
+
259
+
260
+ def test_transaction_select
261
+ result = @trans.transaction_select @data, "**=foo", "key3/key*/2"
262
+ expected = {:key1=>{:key1a=>{0=>"foo"}}, :key3=>{:key3a=>{2=>"val3"}}}
263
+
264
+ assert_equal expected, result
265
+ end
266
+
267
+
268
+ def test_transaction_select_array
269
+ data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
270
+
271
+ result = @trans.transaction_select data_arr, "**=foo", "3/key*/2"
272
+ expected = {1=>{:key1a=>{0=>"foo"}}, 3=>{:key3a=>{2=>"val3"}}}
273
+
274
+ assert_equal expected, result
275
+ end
276
+
277
+
278
+ def test_transaction_select_empty
279
+ assert_equal @data, @trans.transaction_select(@data)
280
+ end
281
+
282
+
283
+ def test_transaction_delete
284
+ result = @trans.transaction_delete @data, "**=foo", "key3/key*/2"
285
+ expected = {
286
+ :key1 => {
287
+ :key1a => {1 => "bar", 2 => "foobar", 3 => {:findme => "thing"}},
288
+ 'key1b' => "findme"
289
+ },
290
+ 'findme' => [123, 456, {:findme => 123456}],
291
+ :key2 => "foobar",
292
+ :key3 => {
293
+ :key3a => {0 => "val1", 1 => "val2"}
294
+ }
295
+ }
296
+
297
+ assert_equal expected, result
298
+ end
299
+
300
+
301
+ def test_transaction_delete_array
302
+ data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
303
+
304
+ result = @trans.transaction_delete data_arr, "**=foo", "3/key*/2"
305
+ expected = {
306
+ 0 => [123, 456, {:findme => 123456}],
307
+ 1 => {
308
+ :key1a => {1 => "bar", 2 => "foobar", 3 => {:findme => "thing"}},
309
+ 'key1b' => "findme"
310
+ },
311
+ 2 => "foobar",
312
+ 3 => {:key3a => {0 => "val1", 1 => "val2"}}
313
+ }
314
+
315
+ assert_equal expected, result
316
+ end
317
+
318
+
319
+ def test_transaction_delete_many_from_embedded_data
320
+ result = @trans.transaction_delete @data, "key1/key1a/1", "key1/key1a/0"
321
+ expected = {
322
+ :key1a => {2 => "foobar", 3 => {:findme => "thing"}},
323
+ 'key1b' => "findme"
324
+ }
325
+
326
+ assert_equal expected, result[:key1]
327
+ end
328
+
329
+
330
+ def test_transaction_delete_empty
331
+ assert_equal @data, @trans.transaction_delete(@data)
332
+ end
333
+
334
+
335
+ def test_transaction_move
336
+ expected = {:key1=>{}, :key2=>"foobar",
337
+ "mapped"=>{
338
+ "1-a"=>["foo", "bar", "foobar", {}],
339
+ "1-b"=>"findme", "3-a"=>["val1", "val2", "val3"]},
340
+ :key3=>{}, "findme"=>[123, 456, {}],
341
+ "more"=>{"one-findme"=>"thing", "two-findme"=>123456}}
342
+
343
+ data = @trans.transaction_move @data,
344
+ ["**=thing", "more/one-%1"],
345
+ ["key*/key??", "mapped/%1-%3"],
346
+ ["mapped", "remapped"],
347
+ ["**=123456", "more/two-%1"]
348
+ data = @trans.remake_arrays data
349
+
350
+ assert_equal expected, data
351
+ assert_not_equal @data, data
352
+ end
353
+
354
+
355
+ def test_transaction_map
356
+ expected = {
357
+ "mapped"=>{
358
+ "1-a"=>["foo", "bar", "foobar", {}],
359
+ "1-b"=>"findme", "3-a"=>["val1", "val2", "val3"]},
360
+ "more"=>[{:findme=>"thing"}]
361
+ }
362
+
363
+ data = @trans.transaction_map @data,
364
+ ["**=thing", "more/12/%1"],
365
+ ["key*/key??", "mapped/%1-%3"],
366
+ ["mapped", "remapped"]
367
+
368
+ data = @trans.remake_arrays data
369
+
370
+ assert_equal expected, data
371
+ assert_not_equal @data, data
372
+ end
373
+
374
+
375
+ def test_transaction_map_no_target
376
+ expected = {
377
+ "mapped"=>{
378
+ "1-a"=>["foo", "bar", "foobar", {}],
379
+ "1-b"=>"findme", "3-a"=>["val1", "val2", "val3"]},
380
+ :key1=>{:key1a => [{:findme=>"thing"}]}
381
+ }
382
+
383
+ data = @trans.transaction_map @data,
384
+ "**=thing",
385
+ ["key*/key??", "mapped/%1-%3"],
386
+ ["mapped", "remapped"]
387
+
388
+ data = @trans.remake_arrays data
389
+
390
+ assert_equal expected, data
391
+ assert_not_equal @data, data
392
+ end
393
+
394
+
395
+ def test_transaction_move_array_conflicting
396
+ expected = {:key1=>{:key1a=>[], "key1b"=>"findme"},:key2=>"foobar",
397
+ :key3=>{:key3a=>[]}, "findme"=>[123, 456, {:findme=>123456}]}
398
+
399
+ data = @trans.transaction_move @data, ["key*/key??/*", "mapped/%4"]
400
+ data = @trans.remake_arrays data
401
+
402
+ mapped = data.delete "mapped"
403
+
404
+ assert_equal expected, data
405
+ assert_not_equal @data, data
406
+
407
+ assert_equal({:findme=>"thing"}, mapped.last)
408
+
409
+ # Due to unordered hashes, this could be
410
+ # %w{val1 val2 val3} OR %w{foo bar foobar}
411
+ assert_equal [String], mapped[0..2].map{|v| v.class}.uniq
412
+ end
413
+
414
+
415
+ def test_force_assign_paths
416
+ data = {'foo' => 'bar'}
417
+
418
+ new_data = @trans.force_assign_paths data,
419
+ %w{sub thing one} => 'val1',
420
+ %w{sub thing two} => 'val2',
421
+ ['sub', 'other', 3] => 'val3',
422
+ ['sub', 'other', 1] => 'val4',
423
+ ['sub', 'other', 5, 6] => 'val5'
424
+
425
+ assert_equal({'foo' => 'bar'}, data)
426
+
427
+ expected = {
428
+ 'foo' => 'bar',
429
+ 'sub' => {
430
+ 'thing' => {'one' => 'val1', 'two' => 'val2'},
431
+ 'other' => {3 => 'val3', 1 => 'val4', 5 => {6 => 'val5'}}
432
+ }
433
+ }
434
+ assert_equal expected, new_data
435
+
436
+ expected['sub']['other'] = ['val4', 'val3', ['val5']]
437
+ new_data = @trans.remake_arrays new_data
438
+ assert_equal expected, new_data
439
+ end
440
+
441
+
442
+ def test_force_assign_paths_root_array
443
+ data = ['foo', 'bar']
444
+
445
+ new_data = @trans.force_assign_paths data,
446
+ [1, 'thing', 'one'] => 'val1',
447
+ [1, 'thing', 'two'] => 'val2',
448
+ [3, 'other', 3] => 'val3',
449
+ [3, 'other', 1] => 'val4',
450
+ [3, 'other', 5, 6] => 'val5'
451
+
452
+ assert_equal(['foo', 'bar'], data)
453
+
454
+ expected = {
455
+ 0 => 'foo',
456
+ 1 => {'thing' => {'one' => 'val1', 'two' => 'val2'}},
457
+ 3 => {'other' => {3 => 'val3', 1 => 'val4', 5 => {6 => 'val5'}}}
458
+ }
459
+ assert_equal expected, new_data
460
+
461
+ expected[3]['other'] = ['val4', 'val3', ['val5']]
462
+ expected = @trans.hash_to_ary expected
463
+
464
+ new_data = @trans.remake_arrays new_data
465
+ assert_equal expected, new_data
466
+ end
467
+
468
+
469
+ def test_is_integer
470
+ assert @trans.is_integer?("123")
471
+ assert @trans.is_integer?(:"123")
472
+ assert !@trans.is_integer?("foo123")
473
+ assert !@trans.is_integer?(:foo123)
474
+ end
475
+
476
+
477
+ def test_ary_to_hash
478
+ expected = {1 => :a, 0 => :foo, 2 => :b}
479
+ assert_equal expected, @trans.ary_to_hash([:foo, :a, :b])
480
+ end
481
+
482
+
483
+ def test_hash_to_ary
484
+ assert_equal [:foo, :a, :b], @trans.hash_to_ary(1 => :a, 0 => :foo, 2 => :b)
485
+ end
486
+
487
+
488
+ def test_clear
489
+ @trans.delete "foo"
490
+ @trans.select "bar"
491
+
492
+ @trans.clear
493
+
494
+ assert @trans.instance_variable_get(:@actions).empty?
495
+ assert @trans.instance_variable_get(:@make_array).empty?
496
+ end
497
+
498
+
499
+ def test_select
500
+ @trans.select "path1", "path2", "path3"
501
+ assert_equal [[:select, ["path1", "path2", "path3"]]],
502
+ @trans.instance_variable_get(:@actions)
503
+
504
+ @trans.select "path4", "path5"
505
+ assert_equal [[:select, ["path1", "path2", "path3", "path4", "path5"]]],
506
+ @trans.instance_variable_get(:@actions)
507
+ end
508
+
509
+
510
+ def test_delete
511
+ @trans.delete "path1", "path2", "path3"
512
+ assert_equal [[:delete, ["path1", "path2", "path3"]]],
513
+ @trans.instance_variable_get(:@actions)
514
+
515
+ @trans.delete "path4", "path5"
516
+ assert_equal [[:delete, ["path1", "path2", "path3"]],
517
+ [:delete, ["path4", "path5"]]],
518
+ @trans.instance_variable_get(:@actions)
519
+ end
520
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-path
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-27 00:00:00.000000000 Z
12
+ date: 2012-07-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
@@ -67,9 +67,10 @@ files:
67
67
  - test/test_core_ext.rb
68
68
  - test/test_path_match.rb
69
69
  - test/test_path_matcher.rb
70
+ - test/test_path_transaction.rb
70
71
  - test/test_helper.rb
71
72
  - .gemtest
72
- homepage: https://github.com/yaksnrainbows/ruby-path
73
+ homepage: https://yaks.me/ruby-path
73
74
  licenses: []
74
75
  post_install_message:
75
76
  rdoc_options:
@@ -91,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  version: '0'
92
93
  requirements: []
93
94
  rubyforge_project: ruby-path
94
- rubygems_version: 1.8.18
95
+ rubygems_version: 1.8.23
95
96
  signing_key:
96
97
  specification_version: 3
97
98
  summary: Simple path-based search and lookup for Ruby.
@@ -101,3 +102,4 @@ test_files:
101
102
  - test/test_path.rb
102
103
  - test/test_path_match.rb
103
104
  - test/test_path_matcher.rb
105
+ - test/test_path_transaction.rb