pluck_each 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a53f3851ce03e4a77c7900b6d2f7577e62803a12
4
- data.tar.gz: 35a28d803fcf5f219d3927f80140804b86e56b34
3
+ metadata.gz: e38a2e5de6b397dab4950becd1de9a80b703dd4e
4
+ data.tar.gz: 0f3798ecff31d3f35ece1f6ecedcea9ce657a233
5
5
  SHA512:
6
- metadata.gz: 5dc272f28fd7ae2019dce19e1c01ca473e9fa3c17044ed9a9c893a92fbf112052129f865819603d9f8ecf7c72518c194724dba64867790a1ce61b527c82e6653
7
- data.tar.gz: 1fb026a0c2c70bc0e38e05e9e3ea9f3539c81436d8474ae2ffb931824012ee61095c34f841337acd9e1d41f270a9df4d67a485b1443013fbd3d412b780249498
6
+ metadata.gz: 4cfd3aff95b389605210a1721da6361de08ef10ed15f70e269be0f846f48811afa7f4791fa559bdc13def4da414a0545e61a64fb2b1b118c144971740d9f753f
7
+ data.tar.gz: d3009c05a01c391bda8294158abab7fe8d45ad432fefa64304cd99a50f4c0f11d675d68d9c9ad3b050709a4e955eb7fca651bb8f1f6948ff9220d87d0a11178b
@@ -1,3 +1,3 @@
1
1
  module PluckEach
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/pluck_each.rb CHANGED
@@ -14,32 +14,41 @@ module ActiveRecord
14
14
  end
15
15
  end
16
16
 
17
- def pluck_in_batches(*args)
18
- options = args.extract_options!
17
+ def pluck_in_batches(*column_names)
18
+ options = column_names.extract_options!
19
+ string_column_names = column_names.map(&:to_s)
20
+
21
+ # Ensure the primary key is selected so we can use it as an offset
22
+ id_in_columns_requested = string_column_names.include?(primary_key)
23
+ string_column_names.unshift(primary_key) unless id_in_columns_requested
24
+ id_position_in_response = string_column_names.index(primary_key)
25
+
19
26
  relation = self
20
27
  batch_size = options[:batch_size] || 1000
21
- offset = 0
22
28
 
23
- relation = relation.reorder(pluck_batch_order).offset(offset).limit(batch_size)
24
- records = relation.pluck(*args)
29
+ relation = relation.reorder(batch_order).limit(batch_size)
30
+ batch_relation = relation
25
31
 
26
- while records.any?
27
- records_size = records.size
28
- offset += records_size
29
- break if records_size <= 0
32
+ loop do
33
+ batch = batch_relation.pluck(*string_column_names)
34
+ break if batch.empty?
30
35
 
31
- yield records
36
+ primary_key_offset = batch.last.at(id_position_in_response)
32
37
 
33
- break if records_size < batch_size
34
- records = relation.offset(offset).limit(batch_size).pluck(*args)
35
- end
36
- end
38
+ unless id_in_columns_requested
39
+ batch.collect! do |record|
40
+ record.delete_at(id_position_in_response)
41
+ record
42
+ end
43
+ end
37
44
 
38
- private
45
+ batch.flatten! if column_names.size == 1
39
46
 
40
- def pluck_batch_order
41
- "#{quoted_table_name}.#{quoted_primary_key} ASC"
42
- end
47
+ yield batch
43
48
 
49
+ break if batch.size < batch_size
50
+ batch_relation = relation.where(arel_attribute(primary_key).gt(primary_key_offset))
51
+ end
52
+ end
44
53
  end
45
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluck_each
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dewitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-05 00:00:00.000000000 Z
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  requirements: []
148
148
  rubyforge_project:
149
- rubygems_version: 2.2.2
149
+ rubygems_version: 2.5.1
150
150
  signing_key:
151
151
  specification_version: 4
152
152
  summary: pluck_each and plucK_in_batches ... should behave like find_each and find_in_batches