moving_window 1.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 +52 -0
  2. data/lib/moving_window.rb +25 -0
  3. metadata +95 -0
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ ## Moving Window
2
+
3
+ A helper for building scopes that deal with moving windows.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ require 'moving_window'
9
+
10
+ class Review < ActiveRecord::Base
11
+ scope :recent, MovingWindow.scope { 6.months.ago }
12
+ end
13
+
14
+ Review.recent # Returns reviews from the last 6 months.
15
+ ```
16
+
17
+ You can specify an end to the window with an array. The ordering does not matter:
18
+
19
+ ```ruby
20
+ scope :a_while_ago, MovingWindow.scope { [3.months.ago, 6.months.ago] }
21
+ ```
22
+
23
+ By default, `created_at` is used. If you want to specify a different column:
24
+
25
+ ```ruby
26
+ scope :recently_published, MovingWindow.scope(:published_at) { 6.months.ago }
27
+ ```
28
+
29
+ Dates in the future will also work.
30
+
31
+ Note: There's no need to worry about invoking the scope with a lambda. The timestamps will be re-evaluated on each call.
32
+
33
+ ## Manual Invocation
34
+
35
+ You'll find that `.scope` won't work outside of an active record model. Invoke things manually instead:
36
+
37
+ ```ruby
38
+ window = MovingWindow.new { 6.months.ago }
39
+ window.filter(Review, :published_at)
40
+ ```
41
+
42
+ Arel is fully supported:
43
+
44
+ ```ruby
45
+ window.filter(Review.published).limit(5)
46
+ ```
47
+
48
+ ## Contribution
49
+
50
+ Feel free to contribute. No commit is too small.
51
+
52
+ You should follow me: [@cpatuzzo](https://twitter.com/cpatuzzo)
@@ -0,0 +1,25 @@
1
+ class MovingWindow
2
+
3
+ def self.scope(column = :created_at, &block)
4
+ arel = block.binding.eval("self")
5
+ instance = new(&block)
6
+
7
+ lambda { instance.filter(arel, column) }
8
+ end
9
+
10
+ def initialize(&block)
11
+ @block = block
12
+ end
13
+
14
+ def filter(scope, column = :created_at)
15
+ scope.where(["#{column} > ? and #{column} < ?", *timestamps])
16
+ end
17
+
18
+ private
19
+ def timestamps
20
+ from, to = @block.call
21
+ to ||= Time.now
22
+ [from, to].sort
23
+ end
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moving_window
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Patuzzo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A helper for building scopes that deal with moving windows.
63
+ email: chris@patuzzo.co.uk
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - README.md
69
+ - lib/moving_window.rb
70
+ homepage: https://github.com/cpatuzzo/moving_window
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.23
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Moving Window
94
+ test_files: []
95
+ has_rdoc: