moving_window 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.md +15 -1
  2. data/lib/moving_window.rb +22 -3
  3. metadata +2 -2
data/README.md CHANGED
@@ -36,7 +36,7 @@ You'll find that `.scope` won't work outside of an active record model. Invoke t
36
36
 
37
37
  ```ruby
38
38
  window = MovingWindow.new { 6.months.ago }
39
- window.filter(Review, :published_at)
39
+ window.filter(Review, :column => :published_at)
40
40
  ```
41
41
 
42
42
  Arel is fully supported:
@@ -45,6 +45,20 @@ Arel is fully supported:
45
45
  window.filter(Review.published).limit(5)
46
46
  ```
47
47
 
48
+ ## Negation
49
+
50
+ You can find all records that lie outside of the moving window by negating it:
51
+
52
+ ```ruby
53
+ scope :not_recent, MovingWindow.scope { 6.months.ago }.not
54
+ ```
55
+
56
+ If you're calling things manually:
57
+
58
+ ```ruby
59
+ window.filter(Review, :column => :published_at, :negate => true)
60
+ ```
61
+
48
62
  ## Contribution
49
63
 
50
64
  Feel free to contribute. No commit is too small.
data/lib/moving_window.rb CHANGED
@@ -4,22 +4,41 @@ class MovingWindow
4
4
  arel = block.binding.eval("self")
5
5
  instance = new(&block)
6
6
 
7
- lambda { instance.filter(arel, column) }
7
+ Procxy.new(instance, arel, column)
8
8
  end
9
9
 
10
10
  def initialize(&block)
11
11
  @block = block
12
12
  end
13
13
 
14
- def filter(scope, column = :created_at)
15
- scope.where(["#{column} > ? and #{column} < ?", *timestamps])
14
+ def filter(scope, params = {})
15
+ column, qualifier = parse(params)
16
+ scope.where(["#{column} #{qualifier} ? and ?", *timestamps])
16
17
  end
17
18
 
18
19
  private
20
+ def parse(params)
21
+ column = params[:column] || :created_at
22
+ qualifier = params[:negate] ? 'not between' : 'between'
23
+
24
+ [column, qualifier]
25
+ end
26
+
19
27
  def timestamps
20
28
  from, to = @block.call
21
29
  to ||= Time.now
22
30
  [from, to].sort
23
31
  end
24
32
 
33
+ class Procxy < Struct.new(:instance, :arel, :column)
34
+ def call
35
+ instance.filter(arel, :column => column, :negate => @not)
36
+ end
37
+
38
+ def not
39
+ @not = !@not
40
+ self
41
+ end
42
+ end
43
+
25
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moving_window
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
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: 2013-01-11 00:00:00.000000000 Z
12
+ date: 2013-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec