dkubb (owner)

Revisions

gist: 220768 Download_button fork
public
Public Clone URL: git://gist.github.com/220768.git
Embed All Files: show embed
Set operations on a Collection #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Collection#union
----------------
 
 # q1 OR q2
 collection = Person.all(:name => 'Ted Han') | Person.all(:name => 'Dan Kubb')
 
 collection.entries # => SELECT * FROM people WHERE name = 'Ted Han' OR name = 'Dan Kubb'
 
 
Collection#intersection
-----------------------
 
 # q1 AND q2
 collection = Person.all(:name => 'Ted Han') & Person.all(:id => 1)
 
 collection.entries # => SELECT * FROM people WHERE name = 'Ted Han' AND id = 1
 
 
Collection#difference
---------------------
 
 # q1 AND NOT(q2)
 collection = Person.all - Person.all(:name => 'Dan Kubb')
 
 collection.entries # => SELECT * FROM people WHERE NOT(name = 'Dan Kubb')
 
--------------------------------------------------------------------------------
 
This would also have to work with Array and Set, eg:
 
  Person.all - [ Person.get(1), Person.get(2) ]
 
  # => SELECT * FROM people WHERE NOT(id IN(1, 2))
 
It could also be made to work with a Range although I am unsure if this is useful, eg:
 
  Person.all - Person.get(1)..Person.get(10)
 
  # => SELECT * FROM people WHERE NOT(id BETWEEN 1 AND 10)