pluck_each 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +12 -0
- data/lib/pluck_each.rb +45 -0
- data/lib/pluck_each/version.rb +3 -0
- data/pluck_each.gemspec +29 -0
- data/spec/pluck_each_spec.rb +77 -0
- data/spec/spec_helper.rb +44 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a53f3851ce03e4a77c7900b6d2f7577e62803a12
|
4
|
+
data.tar.gz: 35a28d803fcf5f219d3927f80140804b86e56b34
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5dc272f28fd7ae2019dce19e1c01ca473e9fa3c17044ed9a9c893a92fbf112052129f865819603d9f8ecf7c72518c194724dba64867790a1ce61b527c82e6653
|
7
|
+
data.tar.gz: 1fb026a0c2c70bc0e38e05e9e3ea9f3539c81436d8474ae2ffb931824012ee61095c34f841337acd9e1d41f270a9df4d67a485b1443013fbd3d412b780249498
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Brandon Dewitt
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# PluckEach
|
2
|
+
|
3
|
+
ActiveRecord comes with find_each and find_in_batches to batch process records from a database.
|
4
|
+
ActiveRecord also has the method `pluck` which allows the selection of a single field without pulling the entire record into memory.
|
5
|
+
|
6
|
+
This gem combines these ideas and provides `pluck_each` and `pluck_in_batches` to allow batch processing of plucked fields from the db.
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
#
|
10
|
+
# User.create(:first_name => "name1")
|
11
|
+
# User.create(:first_name => "name2")
|
12
|
+
# User.create(:first_name => "name3")
|
13
|
+
# User.create(:first_name => "name4")
|
14
|
+
# User.create(:first_name => "name5")
|
15
|
+
#
|
16
|
+
|
17
|
+
User.all.pluck(:first_name) # => ["name1", "name2", "name3", "name4", "name5"]
|
18
|
+
|
19
|
+
# If the table is large you would want to "page" over these values
|
20
|
+
User.all.pluck_each(:first_name, :batch_size => 2) do |first_name| # default batch_size is 1000
|
21
|
+
# do something with first_name
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'pluck_each'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install pluck_each
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it ( https://github.com/[my-github-username]/pluck_each/fork )
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/pluck_each.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "pluck_each/version"
|
2
|
+
require "active_record"
|
3
|
+
require "active_support/core_ext/array/extract_options"
|
4
|
+
|
5
|
+
module PluckEach
|
6
|
+
end
|
7
|
+
|
8
|
+
module ActiveRecord
|
9
|
+
module Batches
|
10
|
+
|
11
|
+
def pluck_each(*args)
|
12
|
+
pluck_in_batches(*args) do |values|
|
13
|
+
values.each { |value| yield value }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def pluck_in_batches(*args)
|
18
|
+
options = args.extract_options!
|
19
|
+
relation = self
|
20
|
+
batch_size = options[:batch_size] || 1000
|
21
|
+
offset = 0
|
22
|
+
|
23
|
+
relation = relation.reorder(pluck_batch_order).offset(offset).limit(batch_size)
|
24
|
+
records = relation.pluck(*args)
|
25
|
+
|
26
|
+
while records.any?
|
27
|
+
records_size = records.size
|
28
|
+
offset += records_size
|
29
|
+
break if records_size <= 0
|
30
|
+
|
31
|
+
yield records
|
32
|
+
|
33
|
+
break if records_size < batch_size
|
34
|
+
records = relation.offset(offset).limit(batch_size).pluck(*args)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def pluck_batch_order
|
41
|
+
"#{quoted_table_name}.#{quoted_primary_key} ASC"
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
data/pluck_each.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pluck_each/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pluck_each"
|
8
|
+
spec.version = PluckEach::VERSION
|
9
|
+
spec.authors = ["Brandon Dewitt"]
|
10
|
+
spec.email = ["brandonsdewitt@gmail.com"]
|
11
|
+
spec.summary = %q{ pluck_each and plucK_in_batches ... should behave like find_each and find_in_batches }
|
12
|
+
spec.description = %q{ pluck_each and plucK_in_batches ... should behave like find_each and find_in_batches }
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", "> 3.2.0"
|
22
|
+
spec.add_dependency "activesupport", "> 3.0.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "minitest"
|
26
|
+
spec.add_development_dependency "mocha"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
describe PluckEach do
|
5
|
+
File.delete(File.expand_path(File.dirname(__FILE__) + "/test.db")) rescue nil
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(
|
8
|
+
:adapter => "sqlite3",
|
9
|
+
:database => "spec/test.db"
|
10
|
+
)
|
11
|
+
load_schema
|
12
|
+
|
13
|
+
describe 'API' do
|
14
|
+
it 'adds pluck_each to Arel relation nodes' do
|
15
|
+
User.all.must_respond_to :pluck_each
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'adds pluck_in_batches to Arel relation nodes' do
|
19
|
+
User.all.must_respond_to :pluck_in_batches
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'pluck_each' do
|
24
|
+
before do
|
25
|
+
User.delete_all
|
26
|
+
User.create(:first_name => '1', :last_name => '1')
|
27
|
+
User.create(:first_name => '2', :last_name => '2')
|
28
|
+
User.create(:first_name => '3', :last_name => '3')
|
29
|
+
User.create(:first_name => '4', :last_name => '4')
|
30
|
+
User.create(:first_name => '5', :last_name => '5')
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'plucks only the fields requested' do
|
34
|
+
values = []
|
35
|
+
User.all.pluck_each(:first_name) do |first_name|
|
36
|
+
values << first_name
|
37
|
+
end
|
38
|
+
|
39
|
+
values.sort!
|
40
|
+
values.must_equal ['1', '2', '3', '4', '5']
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'allows batch_size in options to determine batch size' do
|
44
|
+
count = 0
|
45
|
+
values = []
|
46
|
+
User.all.pluck_each(:first_name, :batch_size => 1) do |first_name|
|
47
|
+
values << first_name
|
48
|
+
count += 1
|
49
|
+
end
|
50
|
+
|
51
|
+
count.must_equal(5)
|
52
|
+
values.sort!
|
53
|
+
values.must_equal ['1', '2', '3', '4', '5']
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'pluck_in_batches' do
|
58
|
+
before do
|
59
|
+
User.delete_all
|
60
|
+
User.create(:first_name => '1', :last_name => '1')
|
61
|
+
User.create(:first_name => '2', :last_name => '2')
|
62
|
+
User.create(:first_name => '3', :last_name => '3')
|
63
|
+
User.create(:first_name => '4', :last_name => '4')
|
64
|
+
User.create(:first_name => '5', :last_name => '5')
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'allows batch_size in options to determine batch size' do
|
68
|
+
batch_sizes = []
|
69
|
+
User.all.pluck_in_batches(:first_name, :batch_size => 2) do |first_names|
|
70
|
+
batch_sizes << first_names.size
|
71
|
+
end
|
72
|
+
|
73
|
+
batch_sizes.sort!
|
74
|
+
batch_sizes.must_equal [1, 2, 2]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.require(:default, :development, :test)
|
4
|
+
|
5
|
+
require 'minitest/spec'
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require 'mocha/api'
|
8
|
+
|
9
|
+
class User < ActiveRecord::Base
|
10
|
+
has_many :products
|
11
|
+
end
|
12
|
+
|
13
|
+
class Product < ActiveRecord::Base
|
14
|
+
belongs_to :user
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveRecord::Schema.verbose = false
|
18
|
+
|
19
|
+
def configure_database(config)
|
20
|
+
@database_config = config
|
21
|
+
end
|
22
|
+
|
23
|
+
def load_schema
|
24
|
+
ActiveRecord::Schema.define(:version => 1) do
|
25
|
+
create_table :users do |t|
|
26
|
+
t.string :first_name
|
27
|
+
t.string :last_name
|
28
|
+
end
|
29
|
+
|
30
|
+
create_table :products do |t|
|
31
|
+
t.string :name
|
32
|
+
t.integer :value
|
33
|
+
t.boolean :available, :default => true
|
34
|
+
t.belongs_to :user
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def recreate_database(database)
|
40
|
+
ActiveRecord::Base.establish_connection(@database_config)
|
41
|
+
ActiveRecord::Base.connection.drop_database(database) rescue nil
|
42
|
+
ActiveRecord::Base.connection.create_database(database)
|
43
|
+
ActiveRecord::Base.establish_connection(@database_config.merge(:database => database))
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pluck_each
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brandon Dewitt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '10.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '10.0'
|
111
|
+
description: " pluck_each and plucK_in_batches ... should behave like find_each and
|
112
|
+
find_in_batches "
|
113
|
+
email:
|
114
|
+
- brandonsdewitt@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- lib/pluck_each.rb
|
125
|
+
- lib/pluck_each/version.rb
|
126
|
+
- pluck_each.gemspec
|
127
|
+
- spec/pluck_each_spec.rb
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
homepage: ''
|
130
|
+
licenses:
|
131
|
+
- MIT
|
132
|
+
metadata: {}
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
requirements: []
|
148
|
+
rubyforge_project:
|
149
|
+
rubygems_version: 2.2.2
|
150
|
+
signing_key:
|
151
|
+
specification_version: 4
|
152
|
+
summary: pluck_each and plucK_in_batches ... should behave like find_each and find_in_batches
|
153
|
+
test_files:
|
154
|
+
- spec/pluck_each_spec.rb
|
155
|
+
- spec/spec_helper.rb
|