acts_as_sweepable 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +3 -0
- data/README.md +5 -4
- data/Rakefile +1 -1
- data/acts_as_sweepable.gemspec +2 -2
- data/lib/acts_as_sweepable.rb +13 -18
- data/spec/acts_as_sweepable_spec.rb +38 -12
- metadata +8 -8
data/CHANGELOG.rdoc
CHANGED
data/README.md
CHANGED
@@ -14,20 +14,21 @@ Adds a class method called sweep to ActiveRecord - used to remove old elements.
|
|
14
14
|
|
15
15
|
## Example
|
16
16
|
|
17
|
-
# Will remove elements
|
17
|
+
# Will remove elements that weren't updated from at least 10 days
|
18
18
|
MyClass.sweep(:time => '10d')
|
19
19
|
|
20
|
-
# Will remove elements
|
20
|
+
# Will remove elements that weren't updated from at least 10 hours
|
21
21
|
MyClass.sweep(:time => '10h')
|
22
22
|
|
23
|
-
# Will remove elements
|
23
|
+
# Will remove elements that weren't updated from at least 10 minutes
|
24
24
|
MyClass.sweep(:time => '10m')
|
25
25
|
|
26
26
|
Params accepted by this method:
|
27
27
|
|
28
28
|
* `time` - d(days),h(hours), m(minutes)
|
29
29
|
* `conditions` - additional SQL conditions (like WHERE)
|
30
|
-
* `
|
30
|
+
* `columns` - (default :updated_at) - which columns should it consider when removing
|
31
|
+
* `method` - (default: destroy_all) - which method should it use to destroy objects
|
31
32
|
|
32
33
|
You can also yield a block of code - it will be performed on every object that should be deleted:
|
33
34
|
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('acts_as_sweepable', '0.
|
5
|
+
Echoe.new('acts_as_sweepable', '0.2.0') do |p|
|
6
6
|
p.description = "Adds a class method called sweep to ActiveRecord - used to remove old elements"
|
7
7
|
p.url = "https://github.com/mensfeld/Acts-as-Sweepable"
|
8
8
|
p.author = "Maciej Mensfeld"
|
data/acts_as_sweepable.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "acts_as_sweepable"
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.2.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Maciej Mensfeld"]
|
9
|
-
s.date = "2012-06-
|
9
|
+
s.date = "2012-06-17"
|
10
10
|
s.description = "Adds a class method called sweep to ActiveRecord - used to remove old elements"
|
11
11
|
s.email = "maciej@mensfeld.pl"
|
12
12
|
s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.md", "lib/acts_as_sweepable.rb"]
|
data/lib/acts_as_sweepable.rb
CHANGED
@@ -16,10 +16,10 @@ module Acts
|
|
16
16
|
|
17
17
|
time_ago = options.delete(:time)
|
18
18
|
conditions = options.delete(:conditions)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
columns = [options.delete(:columns) || :updated_at].flatten
|
20
|
+
remove_method = options.delete(:method) || :destroy_all
|
21
|
+
|
22
|
+
|
23
23
|
time = case time_ago
|
24
24
|
when /^(\d+)m$/ then Time.now - $1.to_i.minute
|
25
25
|
when /^(\d+)h$/ then Time.now - $1.to_i.hour
|
@@ -27,26 +27,22 @@ module Acts
|
|
27
27
|
else raise(InvalidTime, time_ago)
|
28
28
|
end
|
29
29
|
|
30
|
-
statement =
|
31
|
-
|
32
|
-
|
33
|
-
statement += " OR created_at < '#{time.to_s(:db)}')"
|
34
|
-
statement = '('+statement
|
30
|
+
statement = []
|
31
|
+
columns.each do |column|
|
32
|
+
statement << "#{column} < '#{time.to_s(:db)}'"
|
35
33
|
end
|
36
34
|
|
37
|
-
|
35
|
+
statement = "(#{statement.join(' OR ')})"
|
38
36
|
|
39
|
-
|
37
|
+
conditions = "AND #{conditions} " if conditions
|
40
38
|
|
41
39
|
# Run on each block of code
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
self.delete_all "#{statement} #{conditions}"
|
46
|
-
else
|
47
|
-
self.destroy_all "#{statement} #{conditions}"
|
40
|
+
if block_given?
|
41
|
+
els = self.where("#{statement} #{conditions}")
|
42
|
+
els.each {|el| yield el }
|
48
43
|
end
|
49
44
|
|
45
|
+
self.send(remove_method, "#{statement} #{conditions}")
|
50
46
|
end
|
51
47
|
end
|
52
48
|
|
@@ -54,4 +50,3 @@ module Acts
|
|
54
50
|
end
|
55
51
|
|
56
52
|
ActiveRecord::Base.send(:include, Acts::AsSweepable)
|
57
|
-
|
@@ -12,19 +12,34 @@ describe CoolElement do
|
|
12
12
|
|
13
13
|
context "when object is old enough" do
|
14
14
|
it "should be sweeped" do
|
15
|
-
el = subject.create(
|
15
|
+
el = subject.create(
|
16
|
+
:created_at => 10.days.ago,
|
17
|
+
:updated_at => 10.days.ago
|
18
|
+
)
|
16
19
|
subject.sweep(:time => '9d')
|
17
20
|
subject.all.count.should eql(0)
|
18
|
-
el = subject.create(
|
21
|
+
el = subject.create(
|
22
|
+
:created_at => 10.hours.ago,
|
23
|
+
:updated_at => 10.hours.ago
|
24
|
+
)
|
19
25
|
subject.sweep(:time => '9h')
|
20
26
|
subject.all.count.should eql(0)
|
21
|
-
el = subject.create(
|
27
|
+
el = subject.create(
|
28
|
+
:created_at => 10.minutes.ago,
|
29
|
+
:updated_at => 10.minutes.ago
|
30
|
+
)
|
22
31
|
subject.sweep(:time => '9m')
|
23
32
|
subject.all.count.should eql(0)
|
24
|
-
el = subject.create(
|
25
|
-
|
33
|
+
el = subject.create(
|
34
|
+
:created_at => 10.minutes.ago,
|
35
|
+
:updated_at => 1.minutes.ago
|
36
|
+
)
|
37
|
+
subject.sweep(:time => '9m', :columns => [:created_at])
|
26
38
|
subject.all.count.should eql(0)
|
27
|
-
el = subject.create(
|
39
|
+
el = subject.create(
|
40
|
+
:created_at => 1.minutes.ago,
|
41
|
+
:updated_at => 10.minutes.ago
|
42
|
+
)
|
28
43
|
subject.sweep(:time => '9m')
|
29
44
|
subject.all.count.should eql(0)
|
30
45
|
end
|
@@ -36,7 +51,7 @@ describe CoolElement do
|
|
36
51
|
subject.sweep(:time => '11d')
|
37
52
|
subject.all.count.should eql(1)
|
38
53
|
el = subject.create(:created_at => 10.minutes.ago)
|
39
|
-
subject.sweep(:time => '11m')
|
54
|
+
subject.sweep(:time => '11m', :columns => :created_at)
|
40
55
|
subject.all.count.should eql(1)
|
41
56
|
el = subject.create(:created_at => 10.hours.ago)
|
42
57
|
subject.sweep(:time => '11h')
|
@@ -46,8 +61,13 @@ describe CoolElement do
|
|
46
61
|
|
47
62
|
context "when we add additional parameter" do
|
48
63
|
it "should be used" do
|
49
|
-
el1 = subject.create(
|
50
|
-
|
64
|
+
el1 = subject.create(
|
65
|
+
:created_at => 10.days.ago, :name => 'First')
|
66
|
+
el2 = subject.create(
|
67
|
+
:created_at => 10.days.ago,
|
68
|
+
:updated_at => 10.days.ago,
|
69
|
+
:name => 'Second'
|
70
|
+
)
|
51
71
|
subject.sweep(:time => '9d', :conditions => 'name LIKE "Second"')
|
52
72
|
subject.all.count.should eql(1)
|
53
73
|
subject.all.first.name.should eql "First"
|
@@ -64,7 +84,7 @@ describe CoolElement do
|
|
64
84
|
it "should not sweep recently updated objects" do
|
65
85
|
el = subject.create(:updated_at => 1.days.ago, :created_at => 11.days.ago)
|
66
86
|
subject.all.count.should eql(1)
|
67
|
-
subject.sweep(:time => '9d'
|
87
|
+
subject.sweep(:time => '9d')
|
68
88
|
subject.all.count.should eql(1)
|
69
89
|
end
|
70
90
|
end
|
@@ -72,7 +92,13 @@ describe CoolElement do
|
|
72
92
|
context "When we sweep smthg" do
|
73
93
|
it "should be able to perform on each" do
|
74
94
|
subject.expects(:destroy_all)
|
75
|
-
2.times
|
95
|
+
2.times do |i|
|
96
|
+
subject.create(
|
97
|
+
:created_at => 10.days.ago,
|
98
|
+
:updated_at => 10.days.ago,
|
99
|
+
:name => i.to_s
|
100
|
+
)
|
101
|
+
end
|
76
102
|
saved = []
|
77
103
|
subject.sweep(:time => '1d') do |el|
|
78
104
|
saved << el.name
|
@@ -86,7 +112,7 @@ describe CoolElement do
|
|
86
112
|
it 'should use it instead of destroy' do
|
87
113
|
2.times { |i| subject.create(:created_at => 10.days.ago, :name => i.to_s) }
|
88
114
|
subject.expects(:delete_all)
|
89
|
-
subject.sweep(:time => '1d', :
|
115
|
+
subject.sweep(:time => '1d', :method => :delete_all)
|
90
116
|
end
|
91
117
|
end
|
92
118
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_sweepable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-17 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &7499640 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.0.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *7499640
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mocha
|
27
|
-
requirement: &
|
27
|
+
requirement: &7498720 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *7498720
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: active_record
|
38
|
-
requirement: &
|
38
|
+
requirement: &7495540 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *7495540
|
47
47
|
description: Adds a class method called sweep to ActiveRecord - used to remove old
|
48
48
|
elements
|
49
49
|
email: maciej@mensfeld.pl
|