model_iterator 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/model_iterator.rb +30 -3
- data/model_iterator.gemspec +2 -2
- data/test/iterate_test.rb +32 -1
- metadata +3 -3
data/lib/model_iterator.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Iterates over large models, storing state in Redis.
|
2
2
|
class ModelIterator
|
3
|
-
VERSION = "1.0.
|
3
|
+
VERSION = "1.0.3"
|
4
4
|
|
5
5
|
class MaxIterations < StandardError
|
6
6
|
attr_reader :iterator
|
@@ -144,7 +144,7 @@ class ModelIterator
|
|
144
144
|
each_set do |records|
|
145
145
|
records.each do |record|
|
146
146
|
block.call(record)
|
147
|
-
|
147
|
+
update_current_id(record)
|
148
148
|
end
|
149
149
|
end
|
150
150
|
cleanup
|
@@ -220,6 +220,33 @@ class ModelIterator
|
|
220
220
|
opt[:joins] = @joins if @joins
|
221
221
|
opt
|
222
222
|
end
|
223
|
-
end
|
224
223
|
|
224
|
+
# Updates the current ID for the given record.
|
225
|
+
#
|
226
|
+
# record - A single record from the iteration.
|
227
|
+
#
|
228
|
+
# Returns nothing.
|
229
|
+
def update_current_id(record)
|
230
|
+
@current_id = record.send(@id_field)
|
231
|
+
end
|
232
|
+
|
233
|
+
# Pass this to ModelIterator if you don't want to store state anywhere.
|
234
|
+
class NullRedis
|
235
|
+
@instance = new
|
236
|
+
|
237
|
+
def self.new
|
238
|
+
@instance
|
239
|
+
end
|
240
|
+
|
241
|
+
def get(key)
|
242
|
+
0
|
243
|
+
end
|
244
|
+
|
245
|
+
def set(key, value)
|
246
|
+
end
|
247
|
+
|
248
|
+
def del(key)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
225
252
|
|
data/model_iterator.gemspec
CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
|
|
12
12
|
## If your rubyforge_project name is different, then edit it and comment out
|
13
13
|
## the sub! line in the Rakefile
|
14
14
|
s.name = 'model_iterator'
|
15
|
-
s.version = '1.0.
|
16
|
-
s.date = '2013-
|
15
|
+
s.version = '1.0.3'
|
16
|
+
s.date = '2013-05-01'
|
17
17
|
s.rubyforge_project = 'model_iterator'
|
18
18
|
|
19
19
|
## Make sure your summary is short. The description may be as long
|
data/test/iterate_test.rb
CHANGED
@@ -8,7 +8,8 @@ class IterateTest < ModelIterator::TestCase
|
|
8
8
|
|
9
9
|
def test_loops_through_all_records
|
10
10
|
names = []
|
11
|
-
|
11
|
+
redis = RedisClient.new
|
12
|
+
iter = ModelIterator.new Model, :redis => redis, :limit => 1
|
12
13
|
iter.each do |m|
|
13
14
|
names << m.name
|
14
15
|
end
|
@@ -16,6 +17,36 @@ class IterateTest < ModelIterator::TestCase
|
|
16
17
|
assert_equal %w(a b c), names
|
17
18
|
end
|
18
19
|
|
20
|
+
def test_loops_through_all_sets
|
21
|
+
names = []
|
22
|
+
redis = RedisClient.new
|
23
|
+
iter = ModelIterator.new Model, :redis => redis, :limit => 1
|
24
|
+
iter.each_set do |records|
|
25
|
+
names << records.map(&:name)
|
26
|
+
iter.update_current_id(records.last)
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_equal [%w(a), %w(b), %w(c)], names
|
30
|
+
|
31
|
+
iter = ModelIterator.new Model, :redis => redis, :limit => 1
|
32
|
+
assert_equal 3, iter.current_id
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_loops_through_all_sets
|
36
|
+
names = []
|
37
|
+
redis = ModelIterator::NullRedis.new
|
38
|
+
iter = ModelIterator.new Model, :redis => redis, :limit => 1
|
39
|
+
iter.each_set do |records|
|
40
|
+
names << records.map(&:name)
|
41
|
+
iter.update_current_id(records.last)
|
42
|
+
end
|
43
|
+
|
44
|
+
assert_equal [%w(a), %w(b), %w(c)], names
|
45
|
+
|
46
|
+
iter = ModelIterator.new Model, :redis => redis, :limit => 1
|
47
|
+
assert_equal 0, iter.current_id
|
48
|
+
end
|
49
|
+
|
19
50
|
def test_loops_through_filtered_records
|
20
51
|
names = []
|
21
52
|
iter = ModelIterator.new Model, 'name != ?', 'a',
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_iterator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
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-
|
12
|
+
date: 2013-05-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -74,7 +74,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
74
|
version: '0'
|
75
75
|
segments:
|
76
76
|
- 0
|
77
|
-
hash:
|
77
|
+
hash: 2632868807396715918
|
78
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
79
|
none: false
|
80
80
|
requirements:
|