batchelor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/batchelor.rb +30 -0
  2. metadata +48 -0
@@ -0,0 +1,30 @@
1
+ module Batchelor
2
+ def find_in_batches(opts={})
3
+ return unless block_given?
4
+ batch_size = opts.delete(:batch_size) || 1000
5
+ num_offset = 0
6
+
7
+ relation = self.reorder(self.primary_key).limit(batch_size)
8
+ unless opts.empty?
9
+ raise "You can't specify an order, it's forced to be the primary key" if opts[:order].present?
10
+ raise "You can't specify a limit, it's forced to be the batch_size" if opts[:limit].present?
11
+ raise "You can't specify a start in this implementation" if opts[:start].present?
12
+
13
+ relation = relation.apply_finder_options(opts)
14
+ end
15
+
16
+ while (records = relation.offset(num_offset).all).any?
17
+ yield(records)
18
+
19
+ num_offset += records.count
20
+ end
21
+ end
22
+
23
+ def find_each(opts={})
24
+ return unless block_given?
25
+
26
+ find_in_batches(opts) do |records|
27
+ records.each { |record| yield(record) }
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batchelor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Rosenberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! " Override find_in_batches and find_each in ActiveRecord decendant
15
+ classes that do not have an integer primary key\n as these will not work when
16
+ using PostgreSQL.\n"
17
+ email: aarongrosenberg@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/batchelor.rb
23
+ homepage: https://github.com/mojotech/batchelor
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Override find_in_batches and find_each in ActiveRecord decendant classes
47
+ that do not have an integer primary key
48
+ test_files: []