acts_as_sweepable 0.1.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.
- data/MIT-LICENSE +20 -0
- data/Manifest +8 -0
- data/README.md +49 -0
- data/Rakefile +13 -0
- data/acts_as_sweepable.gemspec +37 -0
- data/init.rb +2 -0
- data/lib/acts_as_sweepable.rb +40 -0
- data/spec/acts_as_sweepable_spec.rb +83 -0
- data/spec/spec_helper.rb +20 -0
- data.tar.gz.sig +2 -0
- metadata +112 -0
- metadata.gz.sig +0 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Maciej Mensfeld
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Acts as Sweepable
|
2
|
+
|
3
|
+
## Install
|
4
|
+
|
5
|
+
gem install acts_as_sweepable
|
6
|
+
|
7
|
+
and in your Gemfile:
|
8
|
+
|
9
|
+
gem 'acts_as_sweepable'
|
10
|
+
|
11
|
+
## About
|
12
|
+
|
13
|
+
Adds a class method called sweep to ActiveRecord - used to remove old elements.
|
14
|
+
|
15
|
+
## Example
|
16
|
+
|
17
|
+
# Will remove elements older than 10 days
|
18
|
+
MyClass.sweep('10d')
|
19
|
+
|
20
|
+
# Will remove elements older than 10 hours
|
21
|
+
MyClass.sweep('10h')
|
22
|
+
|
23
|
+
# Will remove elements older than 10 minutes
|
24
|
+
MyClass.sweep('10m')
|
25
|
+
|
26
|
+
Params accepted by this method:
|
27
|
+
|
28
|
+
* `time_ago` - d(days),h(hours), m(minutes)
|
29
|
+
* `conditions` - additional SQL conditions (like WHERE)
|
30
|
+
* `created_or_updated` - (default true) - should it use updated_at and created_at to determine if object is old enough or should it use just created_at.
|
31
|
+
|
32
|
+
You can also yield a block of code - it will be performed on every object that should be deleted:
|
33
|
+
|
34
|
+
MyClass.sweep('10d') do |el|
|
35
|
+
el.do_smhtng
|
36
|
+
end
|
37
|
+
|
38
|
+
## Note on Patches/Pull Requests
|
39
|
+
|
40
|
+
* Fork the project.
|
41
|
+
* Make your feature addition or bug fix.
|
42
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
43
|
+
* Commit, do not mess with Rakefile, version, or history.
|
44
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
45
|
+
* Send me a pull request. Bonus points for topic branches.
|
46
|
+
|
47
|
+
## Copyright
|
48
|
+
|
49
|
+
Copyright (c) 2011 Maciej Mensfeld. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('acts_as_sweepable', '0.1.0') do |p|
|
6
|
+
p.description = "Adds a class method called sweep to ActiveRecord - used to remove old elements"
|
7
|
+
p.url = "https://github.com/mensfeld/acts_as_sweepable"
|
8
|
+
p.author = "Maciej Mensfeld"
|
9
|
+
p.email = "maciej@mensfeld.pl"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = ["rspec >=2.0.0", "active_record"]
|
12
|
+
end
|
13
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{acts_as_sweepable}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Maciej Mensfeld"]
|
9
|
+
s.cert_chain = ["/home/mencio/.cert_keys/gem-public_cert.pem"]
|
10
|
+
s.date = %q{2011-04-09}
|
11
|
+
s.description = %q{Adds a class method called sweep to ActiveRecord - used to remove old elements}
|
12
|
+
s.email = %q{maciej@mensfeld.pl}
|
13
|
+
s.extra_rdoc_files = ["README.md", "lib/acts_as_sweepable.rb"]
|
14
|
+
s.files = ["MIT-LICENSE", "README.md", "Rakefile", "init.rb", "lib/acts_as_sweepable.rb", "spec/acts_as_sweepable_spec.rb", "spec/spec_helper.rb", "Manifest", "acts_as_sweepable.gemspec"]
|
15
|
+
s.homepage = %q{https://github.com/mensfeld/acts_as_sweepable}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Acts_as_sweepable", "--main", "README.md"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{acts_as_sweepable}
|
19
|
+
s.rubygems_version = %q{1.5.2}
|
20
|
+
s.signing_key = %q{/home/mencio/.cert_keys/gem-private_key.pem}
|
21
|
+
s.summary = %q{Adds a class method called sweep to ActiveRecord - used to remove old elements}
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
s.specification_version = 3
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
|
28
|
+
s.add_development_dependency(%q<active_record>, [">= 0"])
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
31
|
+
s.add_dependency(%q<active_record>, [">= 0"])
|
32
|
+
end
|
33
|
+
else
|
34
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
35
|
+
s.add_dependency(%q<active_record>, [">= 0"])
|
36
|
+
end
|
37
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
module Acts
|
3
|
+
module AsSweepable
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def sweep(time_ago = nil, conditions = nil, created_or_updated = true)
|
11
|
+
time = case time_ago
|
12
|
+
when /^(\d+)m$/ then Time.now - $1.to_i.minute
|
13
|
+
when /^(\d+)h$/ then Time.now - $1.to_i.hour
|
14
|
+
when /^(\d+)d$/ then Time.now - $1.to_i.day
|
15
|
+
else Time.now - 1.hour
|
16
|
+
end
|
17
|
+
|
18
|
+
statement = "updated_at < '#{time.to_s(:db)}'"
|
19
|
+
|
20
|
+
if created_or_updated
|
21
|
+
statement += " OR created_at < '#{time.to_s(:db)}')"
|
22
|
+
statement = '('+statement
|
23
|
+
end
|
24
|
+
|
25
|
+
conditions = "AND #{conditions} " if conditions
|
26
|
+
|
27
|
+
els = self.where("#{statement} #{conditions}")
|
28
|
+
|
29
|
+
# Run on each block of code
|
30
|
+
els.each {|el| yield el } if block_given?
|
31
|
+
|
32
|
+
self.delete_all "#{statement} #{conditions}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
ActiveRecord::Base.send(:include, Acts::AsSweepable)
|
40
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
ROOT = File.expand_path(File.dirname(__FILE__))
|
4
|
+
|
5
|
+
class CoolElement < ActiveRecord::Base
|
6
|
+
end
|
7
|
+
|
8
|
+
describe CoolElement do
|
9
|
+
subject { CoolElement }
|
10
|
+
|
11
|
+
before(:each){ CoolElement.destroy_all}
|
12
|
+
|
13
|
+
context "when object is old enough" do
|
14
|
+
it "should be sweeped" do
|
15
|
+
el = subject.create(:created_at => 10.days.ago)
|
16
|
+
subject.sweep('9d')
|
17
|
+
subject.all.count.should eql(0)
|
18
|
+
el = subject.create(:created_at => 10.hours.ago)
|
19
|
+
subject.sweep('9h')
|
20
|
+
subject.all.count.should eql(0)
|
21
|
+
el = subject.create(:created_at => 10.minutes.ago)
|
22
|
+
subject.sweep('9m')
|
23
|
+
subject.all.count.should eql(0)
|
24
|
+
el = subject.create(:created_at => 10.minutes.ago, :updated_at => 1.minutes.ago)
|
25
|
+
subject.sweep('9m')
|
26
|
+
subject.all.count.should eql(0)
|
27
|
+
el = subject.create(:created_at => 1.minutes.ago, :updated_at => 10.minutes.ago)
|
28
|
+
subject.sweep('9m')
|
29
|
+
subject.all.count.should eql(0)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when object is not old enough" do
|
34
|
+
it "should not be sweeped" do
|
35
|
+
el = subject.create(:created_at => 10.days.ago)
|
36
|
+
subject.sweep('11d')
|
37
|
+
subject.all.count.should eql(1)
|
38
|
+
el = subject.create(:created_at => 10.minutes.ago)
|
39
|
+
subject.sweep('11m')
|
40
|
+
subject.all.count.should eql(1)
|
41
|
+
el = subject.create(:created_at => 10.hours.ago)
|
42
|
+
subject.sweep('11h')
|
43
|
+
subject.all.count.should eql(2)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when we add additional parameter" do
|
48
|
+
it "should be used" do
|
49
|
+
el1 = subject.create(:created_at => 10.days.ago, :name => 'First')
|
50
|
+
el2 = subject.create(:created_at => 10.days.ago, :name => 'Second')
|
51
|
+
subject.sweep('9d', 'name LIKE "Second"')
|
52
|
+
subject.all.count.should eql(1)
|
53
|
+
subject.all.first.name.should eql "First"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "when we use only updated_at" do
|
58
|
+
it "should sweep old objects" do
|
59
|
+
el = subject.create(:updated_at => 10.days.ago)
|
60
|
+
subject.sweep('9d', nil, false)
|
61
|
+
subject.all.count.should eql(0)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should not sweep recently updated objects" do
|
65
|
+
el = subject.create(:updated_at => 1.days.ago, :created_at => 10.days.ago)
|
66
|
+
subject.sweep('9d', nil, false)
|
67
|
+
subject.all.count.should eql(1)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "When we sweep smthg" do
|
72
|
+
it "should be able to perform on each" do
|
73
|
+
2.times { |i| subject.create(:created_at => 10.days.ago, :name => i.to_s) }
|
74
|
+
saved = []
|
75
|
+
subject.sweep('1d') do |el|
|
76
|
+
saved << el.name
|
77
|
+
end
|
78
|
+
saved.first.to_i.should eql(0)
|
79
|
+
saved.last.to_i.should eql(1)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'sqlite3'
|
6
|
+
require 'active_record'
|
7
|
+
require 'acts_as_sweepable'
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(
|
10
|
+
:adapter => "sqlite3",
|
11
|
+
:database => ":memory:"
|
12
|
+
)
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define do
|
15
|
+
create_table :cool_elements do |table|
|
16
|
+
table.text :name
|
17
|
+
table.timestamps
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
data.tar.gz.sig
ADDED
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_sweepable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maciej Mensfeld
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain:
|
12
|
+
- |
|
13
|
+
-----BEGIN CERTIFICATE-----
|
14
|
+
MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MQ8wDQYDVQQDDAZtYWNp
|
15
|
+
ZWoxGDAWBgoJkiaJk/IsZAEZFghtZW5zZmVsZDESMBAGCgmSJomT8ixkARkWAnBs
|
16
|
+
MB4XDTExMDQwOTA5NDcyMloXDTEyMDQwODA5NDcyMlowPzEPMA0GA1UEAwwGbWFj
|
17
|
+
aWVqMRgwFgYKCZImiZPyLGQBGRYIbWVuc2ZlbGQxEjAQBgoJkiaJk/IsZAEZFgJw
|
18
|
+
bDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL0+nG3V4/exIeiJ0IN+
|
19
|
+
wVfq8Utcu4Qpo+58EIVMIu3FiK+8w6MBvatZnUrRu12pqWLw9xrUkCiYeRErD+jF
|
20
|
+
AmdggIM/tu9CcjvURXH7VeTzOVA+pnV+eJWMD61o8HljFVcb/nyEYYVKErtr9/O4
|
21
|
+
QrIGv5lnszq1PMj2sBMy2gOP1YnzawncMLmkpp/T5SU4JZ5gAktGMRVz8RxmZzF5
|
22
|
+
6NVqFLbuqSRSU5U//WJvZVJt8dycCGgQzBM4Vi3nkOWyjIF0BANf1TqnlU2u6s8d
|
23
|
+
UK1AoDZfg5feef5e8eqoomHebX1opNGM/SOQhu3LRgax4rJfnl6VS3I2wighohsf
|
24
|
+
AgcCAwEAAaM5MDcwCQYDVR0TBAIwADAdBgNVHQ4EFgQUGlrWBqxVieAPk7NEzBDp
|
25
|
+
kM+iAMMwCwYDVR0PBAQDAgSwMA0GCSqGSIb3DQEBBQUAA4IBAQAJMoyBaJs8boiz
|
26
|
+
lFpbw6MWjk+7ZhqoHpFrWEV4nzb5GzyHZ7GU/pa1fSEQR0SCs+LnTLQbAYNQyUTT
|
27
|
+
O+UsTuA7xzI//v6cSodv3Q9NbfoDlou74xv1NXorWoosQFMpVWrXv+c/1RqU3cq4
|
28
|
+
WUr+rRiveEXG4tXOwkrpX8KH8xVp2vQZcGw3AXPqhzfqDGzpHd6ws3lk+8HoSrSo
|
29
|
+
2L68tDoxraF2Z2toAg9vfFw1+mOeDk1xVIPVcBy3tJxstHfHGHlQuMiRiDQX2b2D
|
30
|
+
YYU8UWVt2841IwB5Dgl4O+atXhe9ZTBO0W32pl4Bq5CP9lhQRT1KL7sxfznJlF7Y
|
31
|
+
BH3YFsdk
|
32
|
+
-----END CERTIFICATE-----
|
33
|
+
|
34
|
+
date: 2011-04-09 00:00:00 +02:00
|
35
|
+
default_executable:
|
36
|
+
dependencies:
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.0.0
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id001
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: active_record
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id002
|
59
|
+
description: Adds a class method called sweep to ActiveRecord - used to remove old elements
|
60
|
+
email: maciej@mensfeld.pl
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
extra_rdoc_files:
|
66
|
+
- README.md
|
67
|
+
- lib/acts_as_sweepable.rb
|
68
|
+
files:
|
69
|
+
- MIT-LICENSE
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- init.rb
|
73
|
+
- lib/acts_as_sweepable.rb
|
74
|
+
- spec/acts_as_sweepable_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
- Manifest
|
77
|
+
- acts_as_sweepable.gemspec
|
78
|
+
has_rdoc: true
|
79
|
+
homepage: https://github.com/mensfeld/acts_as_sweepable
|
80
|
+
licenses: []
|
81
|
+
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options:
|
84
|
+
- --line-numbers
|
85
|
+
- --inline-source
|
86
|
+
- --title
|
87
|
+
- Acts_as_sweepable
|
88
|
+
- --main
|
89
|
+
- README.md
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: "1.2"
|
104
|
+
requirements: []
|
105
|
+
|
106
|
+
rubyforge_project: acts_as_sweepable
|
107
|
+
rubygems_version: 1.5.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Adds a class method called sweep to ActiveRecord - used to remove old elements
|
111
|
+
test_files: []
|
112
|
+
|
metadata.gz.sig
ADDED
Binary file
|