ohm 1.0.0.alpha1 → 1.0.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -456,6 +456,9 @@ You can find a collection of records with the `find` method:
456
456
  # Find all users from Argentina, except those with a suspended account.
457
457
  User.find(:country => "Argentina").except(:status => "suspended")
458
458
 
459
+ # Find all users both from Argentina and Uruguay
460
+ User.find(:country => "Argentina").union(:country => "Uruguay")
461
+
459
462
  Note that calling these methods results in new sets being created
460
463
  on the fly. This is important so that you can perform further operations
461
464
  before reading the items to the client.
data/lib/ohm.rb CHANGED
@@ -294,6 +294,19 @@ module Ohm
294
294
  key.sadd(model.id)
295
295
  end
296
296
 
297
+ # Remove a model directly from the set.
298
+ #
299
+ # Example:
300
+ #
301
+ # user = User.create
302
+ # post = Post.create
303
+ #
304
+ # user.posts.delete(post)
305
+ #
306
+ def delete(model)
307
+ key.srem(model.id)
308
+ end
309
+
297
310
  # Chain new fiters on an existing set.
298
311
  #
299
312
  # Example:
@@ -308,6 +321,34 @@ module Ohm
308
321
  MultiSet.new(keys, namespace, model)
309
322
  end
310
323
 
324
+ # Reduce the set using any number of filters.
325
+ #
326
+ # Example:
327
+ #
328
+ # set = User.find(name: "John")
329
+ # set.except(country: "US")
330
+ #
331
+ # # You can also do it in one line.
332
+ # User.find(name: "John").except(country: "US")
333
+ #
334
+ def except(dict)
335
+ MultiSet.new([key], namespace, model).except(dict)
336
+ end
337
+
338
+ # Do a union to the existing set using any number of filters.
339
+ #
340
+ # Example:
341
+ #
342
+ # set = User.find(name: "John")
343
+ # set.union(name: "Jane")
344
+ #
345
+ # # You can also do it in one line.
346
+ # User.find(name: "John").union(name: "Jane")
347
+ #
348
+ def union(dict)
349
+ MultiSet.new([key], namespace, model).union(dict)
350
+ end
351
+
311
352
  # Replace all the existing elements of a set with a different
312
353
  # collection of models. This happens atomically in a MULTI-EXEC
313
354
  # block.
@@ -372,10 +413,52 @@ module Ohm
372
413
  MultiSet.new(keys, namespace, model)
373
414
  end
374
415
 
416
+ # Reduce the set using any number of filters.
417
+ #
418
+ # Example:
419
+ #
420
+ # set = User.find(name: "John")
421
+ # set.except(country: "US")
422
+ #
423
+ # # You can also do it in one line.
424
+ # User.find(name: "John").except(country: "US")
425
+ #
426
+ def except(dict)
427
+ sdiff.push(*model.filters(dict)).uniq!
428
+
429
+ return self
430
+ end
431
+
432
+ # Do a union to the existing set using any number of filters.
433
+ #
434
+ # Example:
435
+ #
436
+ # set = User.find(name: "John")
437
+ # set.union(name: "Jane")
438
+ #
439
+ # # You can also do it in one line.
440
+ # User.find(name: "John").union(name: "Jane")
441
+ #
442
+ def union(dict)
443
+ sunion.push(*model.filters(dict)).uniq!
444
+
445
+ return self
446
+ end
447
+
375
448
  private
449
+ def sunion
450
+ @sunion ||= []
451
+ end
452
+
453
+ def sdiff
454
+ @sdiff ||= []
455
+ end
456
+
376
457
  def execute
377
458
  key = namespace[:temp][SecureRandom.uuid]
378
459
  key.sinterstore(*keys)
460
+ key.sdiffstore(key, *sdiff) if sdiff.any?
461
+ key.sunionstore(key, *sunion) if sunion.any?
379
462
 
380
463
  begin
381
464
  yield key
data/test/filtering.rb CHANGED
@@ -40,3 +40,25 @@ test "#[]" do |john, jane|
40
40
  assert_equal john, set[john.id]
41
41
  assert_equal jane, set[jane.id]
42
42
  end
43
+
44
+ test "#except" do |john, jane|
45
+ out = User.create(status: "inactive", lname: "Doe")
46
+
47
+ res = User.find(lname: "Doe").except(status: "inactive")
48
+
49
+ assert_equal 2, res.size
50
+ assert res.include?(john)
51
+ assert res.include?(jane)
52
+ end
53
+
54
+ test "#union" do |john, jane|
55
+ included = User.create(status: "inactive", lname: "Doe")
56
+ excluded = User.create(status: "super", lname: "Doe")
57
+
58
+ res = User.find(status: "active").union(status: "inactive")
59
+
60
+ assert_equal 3, res.size
61
+ assert res.include?(john)
62
+ assert res.include?(jane)
63
+ assert res.include?(included)
64
+ end
data/test/model.rb CHANGED
@@ -452,6 +452,18 @@ test "filter elements" do
452
452
  assert [] == @event.attendees.find(:initial => "Z").to_a
453
453
  end
454
454
 
455
+ test "delete elements" do
456
+ @event.save
457
+ @event.attendees.add(@person1)
458
+ @event.attendees.add(@person2)
459
+
460
+ assert_equal 2, @event.attendees.size
461
+
462
+ @event.attendees.delete(@person2)
463
+ assert_equal 1, @event.attendees.size
464
+ end
465
+
466
+
455
467
  test "not be available if the model is new" do
456
468
  assert_raise Ohm::MissingID do
457
469
  @event.attendees
@@ -60,6 +60,6 @@ t2 = Benchmark.realtime do
60
60
  end
61
61
  end
62
62
 
63
- test "the pipelined approach should be 2 times faster for 100 records" do
64
- assert(t2 / t1 >= 2)
63
+ test "the pipelined approach should be faster for 100 records" do
64
+ assert(t2 > t1)
65
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha1
4
+ version: 1.0.0.alpha2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-03-19 00:00:00.000000000 Z
13
+ date: 2012-03-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: nest
17
- requirement: &2156510860 !ruby/object:Gem::Requirement
17
+ requirement: &2156281500 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '1.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2156510860
25
+ version_requirements: *2156281500
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: scrivener
28
- requirement: &2156526680 !ruby/object:Gem::Requirement
28
+ requirement: &2156297300 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 0.0.3
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2156526680
36
+ version_requirements: *2156297300
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: cutest
39
- requirement: &2156526220 !ruby/object:Gem::Requirement
39
+ requirement: &2156296800 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0.1'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2156526220
47
+ version_requirements: *2156296800
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: batch
50
- requirement: &2156525700 !ruby/object:Gem::Requirement
50
+ requirement: &2156296280 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: 0.0.1
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2156525700
58
+ version_requirements: *2156296280
59
59
  description: Ohm is a library that allows to store an object in Redis, a persistent
60
60
  key-value database. It includes an extensible list of validations and has very good
61
61
  performance.