pluck_all 2.2.1 → 2.3.3
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 +4 -4
- data/.github/workflows/ruby.yml +116 -0
- data/.gitignore +10 -10
- data/.rubocop.yml +1227 -1227
- data/CHANGELOG.md +12 -0
- data/CODE_OF_CONDUCT.md +48 -48
- data/LICENSE.txt +21 -21
- data/README.md +187 -167
- data/Rakefile +22 -22
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/gemfiles/active_record_32.gemfile +16 -13
- data/gemfiles/active_record_42.gemfile +18 -13
- data/gemfiles/active_record_50.gemfile +18 -13
- data/gemfiles/active_record_51.gemfile +18 -13
- data/gemfiles/active_record_52.gemfile +18 -13
- data/gemfiles/active_record_60.gemfile +18 -13
- data/gemfiles/active_record_61.gemfile +18 -13
- data/gemfiles/mongoid_54.gemfile +11 -11
- data/gemfiles/mongoid_64.gemfile +11 -11
- data/gemfiles/{mongoid_70.gemfile → mongoid_73.gemfile} +11 -11
- data/lib/pluck_all/hooks.rb +21 -21
- data/lib/pluck_all/models/active_record_extension.rb +138 -131
- data/lib/pluck_all/models/mongoid_extension.rb +88 -70
- data/lib/pluck_all/models/patches/deserialize.rb +13 -13
- data/lib/pluck_all/version.rb +4 -4
- data/lib/pluck_all.rb +3 -3
- data/pluck_all.gemspec +43 -43
- metadata +7 -7
- data/.travis.yml +0 -73
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
## Change Log
|
|
2
2
|
|
|
3
|
+
### [v2.3.1](https://github.com/khiav223577/pluck_all/compare/v2.3.0...v2.3.1) 2021/06/10
|
|
4
|
+
- [#53](https://github.com/khiav223577/pluck_all/pull/53) Fix: ActiveRecord::ImmutableRelation (@khiav223577)
|
|
5
|
+
|
|
6
|
+
### [v2.3.0](https://github.com/khiav223577/pluck_all/compare/v2.2.1...v2.3.0) 2021/06/10
|
|
7
|
+
- [#52](https://github.com/khiav223577/pluck_all/pull/52) Support `globalize` gem (@khiav223577)
|
|
8
|
+
- [#51](https://github.com/khiav223577/pluck_all/pull/51) Fix: broken test cases caused by `mimemagic` (@khiav223577)
|
|
9
|
+
- [#50](https://github.com/khiav223577/pluck_all/pull/50) Migrating from Travis CI to GitHub Actions (@khiav223577)
|
|
10
|
+
- [#49](https://github.com/khiav223577/pluck_all/pull/49) Fix: test files should not be included in coverage (@khiav223577)
|
|
11
|
+
|
|
12
|
+
### [v2.2.1](https://github.com/khiav223577/pluck_all/compare/v2.1.0...v2.2.1) 2020/12/29
|
|
13
|
+
- [#48](https://github.com/khiav223577/pluck_all/pull/48) fix Rails 6.1 `map!` deprecation warning (@klausbadelt)
|
|
14
|
+
|
|
3
15
|
### [v2.1.0](https://github.com/khiav223577/pluck_all/compare/v2.0.4...v2.1.0) 2020/10/19
|
|
4
16
|
- [#47](https://github.com/khiav223577/pluck_all/pull/47) Use instantiate method to initialize model without calling callbacks (@khiav223577)
|
|
5
17
|
- [#45](https://github.com/khiav223577/pluck_all/pull/45) Support Ruby 2.7 (@khiav223577)
|
data/CODE_OF_CONDUCT.md
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
# Contributor Code of Conduct
|
|
2
|
-
|
|
3
|
-
As contributors and maintainers of this project, and in the interest of
|
|
4
|
-
fostering an open and welcoming community, we pledge to respect all people who
|
|
5
|
-
contribute through reporting issues, posting feature requests, updating
|
|
6
|
-
documentation, submitting pull requests or patches, and other activities.
|
|
7
|
-
|
|
8
|
-
We are committed to making participation in this project a harassment-free
|
|
9
|
-
experience for everyone, regardless of level of experience, gender, gender
|
|
10
|
-
identity and expression, sexual orientation, disability, personal appearance,
|
|
11
|
-
body size, race, ethnicity, age, religion, or nationality.
|
|
12
|
-
|
|
13
|
-
Examples of unacceptable behavior by participants include:
|
|
14
|
-
|
|
15
|
-
* The use of sexualized language or imagery
|
|
16
|
-
* Personal attacks
|
|
17
|
-
* Trolling or insulting/derogatory comments
|
|
18
|
-
* Public or private harassment
|
|
19
|
-
* Publishing other's private information, such as physical or electronic
|
|
20
|
-
addresses, without explicit permission
|
|
21
|
-
* Other unethical or unprofessional conduct
|
|
22
|
-
|
|
23
|
-
Project maintainers have the right and responsibility to remove, edit, or
|
|
24
|
-
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
25
|
-
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
26
|
-
permanently any contributor for other behaviors that they deem inappropriate,
|
|
27
|
-
threatening, offensive, or harmful.
|
|
28
|
-
|
|
29
|
-
By adopting this Code of Conduct, project maintainers commit themselves to
|
|
30
|
-
fairly and consistently applying these principles to every aspect of managing
|
|
31
|
-
this project. Project maintainers who do not follow or enforce the Code of
|
|
32
|
-
Conduct may be permanently removed from the project team.
|
|
33
|
-
|
|
34
|
-
This code of conduct applies both within project spaces and in public spaces
|
|
35
|
-
when an individual is representing the project or its community.
|
|
36
|
-
|
|
37
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
38
|
-
reported by contacting a project maintainer at mrtmrt15xn@yahoo.com.tw. All
|
|
39
|
-
complaints will be reviewed and investigated and will result in a response that
|
|
40
|
-
is deemed necessary and appropriate to the circumstances. Maintainers are
|
|
41
|
-
obligated to maintain confidentiality with regard to the reporter of an
|
|
42
|
-
incident.
|
|
43
|
-
|
|
44
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
45
|
-
version 1.3.0, available at
|
|
46
|
-
[http://contributor-covenant.org/version/1/3/0/][version]
|
|
47
|
-
|
|
48
|
-
[homepage]: http://contributor-covenant.org
|
|
1
|
+
# Contributor Code of Conduct
|
|
2
|
+
|
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
|
7
|
+
|
|
8
|
+
We are committed to making participation in this project a harassment-free
|
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
|
12
|
+
|
|
13
|
+
Examples of unacceptable behavior by participants include:
|
|
14
|
+
|
|
15
|
+
* The use of sexualized language or imagery
|
|
16
|
+
* Personal attacks
|
|
17
|
+
* Trolling or insulting/derogatory comments
|
|
18
|
+
* Public or private harassment
|
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
|
20
|
+
addresses, without explicit permission
|
|
21
|
+
* Other unethical or unprofessional conduct
|
|
22
|
+
|
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
27
|
+
threatening, offensive, or harmful.
|
|
28
|
+
|
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
|
32
|
+
Conduct may be permanently removed from the project team.
|
|
33
|
+
|
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
|
35
|
+
when an individual is representing the project or its community.
|
|
36
|
+
|
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
38
|
+
reported by contacting a project maintainer at mrtmrt15xn@yahoo.com.tw. All
|
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
|
42
|
+
incident.
|
|
43
|
+
|
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
45
|
+
version 1.3.0, available at
|
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
|
47
|
+
|
|
48
|
+
[homepage]: http://contributor-covenant.org
|
|
49
49
|
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/LICENSE.txt
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2016 khiav reoy
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 khiav reoy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,167 +1,187 @@
|
|
|
1
|
-
# PluckAll
|
|
2
|
-
|
|
3
|
-
[](http://rubygems.org/gems/pluck_all)
|
|
4
|
-
[](http://rubygems.org/gems/pluck_all)
|
|
6
|
-
[](https://codeclimate.com/github/khiav223577/pluck_all)
|
|
7
|
-
[](https://codeclimate.com/github/khiav223577/pluck_all/coverage)
|
|
8
|
-
|
|
9
|
-
Pluck multiple columns/attributes in Rails 3, 4, 5, 6, and can return data as hash instead of only array. Also supports `Mongoid`.
|
|
10
|
-
|
|
11
|
-
This Gem stands on the shoulders of this article: [Plucking Multiple Columns in Rails 3](https://meltingice.dev/2013/06/11/pluck-multiple-columns-rails/).
|
|
12
|
-
And modified to support not only Rail 3.
|
|
13
|
-
|
|
14
|
-
If you have a Rails 3 project, and want to pluck not only one column,
|
|
15
|
-
feel free to use this gem and no need to worry about upgrading to Rails 4, 5, 6 in the future will break this.
|
|
16
|
-
|
|
17
|
-
## Supports
|
|
18
|
-
- Ruby 2.2 ~ 2.7
|
|
19
|
-
- Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0, 6.1
|
|
20
|
-
|
|
21
|
-
## Installation
|
|
22
|
-
|
|
23
|
-
Add this line to your application's Gemfile:
|
|
24
|
-
|
|
25
|
-
```ruby
|
|
26
|
-
gem 'pluck_all'
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
And then execute:
|
|
30
|
-
|
|
31
|
-
$ bundle
|
|
32
|
-
|
|
33
|
-
Or install it yourself as:
|
|
34
|
-
|
|
35
|
-
$ gem install pluck_all
|
|
36
|
-
|
|
37
|
-
## Usage
|
|
38
|
-
|
|
39
|
-
### pluck to array
|
|
40
|
-
|
|
41
|
-
Behaves the same as `#pluck` method, but you can use it to pluck multiple columns in Rails 3
|
|
42
|
-
|
|
43
|
-
```rb
|
|
44
|
-
User.where('id < 3').pluck_array(:id, :account)
|
|
45
|
-
# => [[1, 'account1'], [2, 'account2']]
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### pluck to hash
|
|
49
|
-
|
|
50
|
-
Similar to `#pluck` method, but return array of hashes instead.
|
|
51
|
-
|
|
52
|
-
```rb
|
|
53
|
-
User.where('id < 3').pluck_all(:id, :account)
|
|
54
|
-
# => [{"id"=>1, "account"=>"account1"}, {"id"=>2, "account"=>"account2"}]
|
|
55
|
-
|
|
56
|
-
User.where('id < 3').pluck_all(:id, 'account AS name')
|
|
57
|
-
# => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
|
|
58
|
-
|
|
59
|
-
User.where('id < 3').pluck_all('id, account AS name')
|
|
60
|
-
# => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Support Mongoid
|
|
64
|
-
```rb
|
|
65
|
-
class User
|
|
66
|
-
include Mongoid::Document
|
|
67
|
-
|
|
68
|
-
field :name, type: String
|
|
69
|
-
field :age, type: Integer
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
User.pluck_all(:name, :age)
|
|
73
|
-
# => [
|
|
74
|
-
# {'name' => 'Pearl Shi' , 'age' => 18},
|
|
75
|
-
# {'name' => 'Rumble Huang', 'age' => 20},
|
|
76
|
-
# {'name' => 'Khiav Reoy' , 'age' => 20},
|
|
77
|
-
# ]
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
## Benchmark
|
|
82
|
-
### Compare with `map` and `as_json`
|
|
83
|
-
|
|
84
|
-
`pluck_all` return raw `hash` data without loading a bunch of records, in that having better performace than using `map` and `as_json`. The following is the benchmark test on 191,093 users, where `users` table have 51 columns.
|
|
85
|
-
|
|
86
|
-
```rb
|
|
87
|
-
user system total real
|
|
88
|
-
map 36.110000 61.200000 97.310000 ( 99.535375)
|
|
89
|
-
select + map 10.530000 0.660000 11.190000 ( 12.550974)
|
|
90
|
-
select + as_json 49.040000 1.120000 50.160000 ( 55.417534)
|
|
91
|
-
pluck_all 3.310000 0.100000 3.410000 ( 3.527775)
|
|
92
|
-
```
|
|
93
|
-
Test by `benchmark-ips` and `limit 100` in each iteration:
|
|
94
|
-
```
|
|
95
|
-
Warming up --------------------------------------
|
|
96
|
-
map 1.000 i/100ms
|
|
97
|
-
select + map 28.000 i/100ms
|
|
98
|
-
select + as_json 7.000 i/100ms
|
|
99
|
-
pluck_all 54.000 i/100ms
|
|
100
|
-
Calculating -------------------------------------
|
|
101
|
-
map 14.230 (± 0.0%) i/s - 72.000 in 5.065349s
|
|
102
|
-
select + map 281.638 (± 4.6%) i/s - 1.428k in 5.081216s
|
|
103
|
-
select + as_json 73.241 (± 4.1%) i/s - 371.000 in 5.076235s
|
|
104
|
-
pluck_all 539.057 (± 6.7%) i/s - 2.700k in 5.034858s
|
|
105
|
-
|
|
106
|
-
Comparison:
|
|
107
|
-
pluck_all: 539.1 i/s
|
|
108
|
-
select + map: 281.6 i/s - 1.91x slower
|
|
109
|
-
select + as_json: 73.2 i/s - 7.36x slower
|
|
110
|
-
map: 14.2 i/s - 37.88x slower
|
|
111
|
-
```
|
|
112
|
-
[test script](https://github.com/khiav223577/pluck_all/issues/18)
|
|
113
|
-
|
|
114
|
-
### Compare with [pluck_to_hash](https://github.com/girishso/pluck_to_hash) gem
|
|
115
|
-
|
|
116
|
-
`pluck_all` has better performace since it uses raw `hash` data from `ActiveRecord::Base.connection.select_all`, while `pluck_to_hash` uses `pluck` method, which calls `ActiveRecord::Base.connection.select_all` and transfers the raw `hash` data to `array` format, and then transfer the data to `hash` format again. The following benchmark shows the performance difference:
|
|
117
|
-
|
|
118
|
-
```rb
|
|
119
|
-
user system total real
|
|
120
|
-
pluck_to_hash 2.960000 0.130000 3.090000 ( 3.421640)
|
|
121
|
-
pluck_all 2.160000 0.120000 2.280000 ( 2.605118)
|
|
122
|
-
```
|
|
123
|
-
Tested by `benchmark-ips` and `limit 1000` in each iteration:
|
|
124
|
-
```
|
|
125
|
-
Warming up --------------------------------------
|
|
126
|
-
pluck_to_hash 7.000 i/100ms
|
|
127
|
-
pluck_all 9.000 i/100ms
|
|
128
|
-
Calculating -------------------------------------
|
|
129
|
-
pluck_to_hash 84.526 (± 4.7%) i/s - 427.000 in 5.065792s
|
|
130
|
-
pluck_all 95.133 (± 4.2%) i/s - 477.000 in 5.021555s
|
|
131
|
-
|
|
132
|
-
Comparison:
|
|
133
|
-
pluck_all: 95.1 i/s
|
|
134
|
-
pluck_to_hash: 84.5 i/s - 1.13x slower
|
|
135
|
-
```
|
|
136
|
-
See the [test script](https://github.com/khiav223577/pluck_all/issues/18#issuecomment-325407080) for more details.
|
|
137
|
-
|
|
138
|
-
## Other Support
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
1
|
+
# PluckAll
|
|
2
|
+
|
|
3
|
+
[](http://rubygems.org/gems/pluck_all)
|
|
4
|
+
[](https://github.com/khiav223577/pluck_all/actions)
|
|
5
|
+
[](http://rubygems.org/gems/pluck_all)
|
|
6
|
+
[](https://codeclimate.com/github/khiav223577/pluck_all)
|
|
7
|
+
[](https://codeclimate.com/github/khiav223577/pluck_all/coverage)
|
|
8
|
+
|
|
9
|
+
Pluck multiple columns/attributes in Rails 3, 4, 5, 6, and can return data as hash instead of only array. Also supports `Mongoid`.
|
|
10
|
+
|
|
11
|
+
This Gem stands on the shoulders of this article: [Plucking Multiple Columns in Rails 3](https://meltingice.dev/2013/06/11/pluck-multiple-columns-rails/).
|
|
12
|
+
And modified to support not only Rail 3.
|
|
13
|
+
|
|
14
|
+
If you have a Rails 3 project, and want to pluck not only one column,
|
|
15
|
+
feel free to use this gem and no need to worry about upgrading to Rails 4, 5, 6 in the future will break this.
|
|
16
|
+
|
|
17
|
+
## Supports
|
|
18
|
+
- Ruby 2.2 ~ 2.7, 3.0
|
|
19
|
+
- Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0, 6.1
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Add this line to your application's Gemfile:
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
gem 'pluck_all'
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
And then execute:
|
|
30
|
+
|
|
31
|
+
$ bundle
|
|
32
|
+
|
|
33
|
+
Or install it yourself as:
|
|
34
|
+
|
|
35
|
+
$ gem install pluck_all
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
### pluck to array
|
|
40
|
+
|
|
41
|
+
Behaves the same as `#pluck` method, but you can use it to pluck multiple columns in Rails 3
|
|
42
|
+
|
|
43
|
+
```rb
|
|
44
|
+
User.where('id < 3').pluck_array(:id, :account)
|
|
45
|
+
# => [[1, 'account1'], [2, 'account2']]
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### pluck to hash
|
|
49
|
+
|
|
50
|
+
Similar to `#pluck` method, but return array of hashes instead.
|
|
51
|
+
|
|
52
|
+
```rb
|
|
53
|
+
User.where('id < 3').pluck_all(:id, :account)
|
|
54
|
+
# => [{"id"=>1, "account"=>"account1"}, {"id"=>2, "account"=>"account2"}]
|
|
55
|
+
|
|
56
|
+
User.where('id < 3').pluck_all(:id, 'account AS name')
|
|
57
|
+
# => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
|
|
58
|
+
|
|
59
|
+
User.where('id < 3').pluck_all('id, account AS name')
|
|
60
|
+
# => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Support Mongoid
|
|
64
|
+
```rb
|
|
65
|
+
class User
|
|
66
|
+
include Mongoid::Document
|
|
67
|
+
|
|
68
|
+
field :name, type: String
|
|
69
|
+
field :age, type: Integer
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
User.pluck_all(:name, :age)
|
|
73
|
+
# => [
|
|
74
|
+
# {'name' => 'Pearl Shi' , 'age' => 18},
|
|
75
|
+
# {'name' => 'Rumble Huang', 'age' => 20},
|
|
76
|
+
# {'name' => 'Khiav Reoy' , 'age' => 20},
|
|
77
|
+
# ]
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
## Benchmark
|
|
82
|
+
### Compare with `map` and `as_json`
|
|
83
|
+
|
|
84
|
+
`pluck_all` return raw `hash` data without loading a bunch of records, in that having better performace than using `map` and `as_json`. The following is the benchmark test on 191,093 users, where `users` table have 51 columns.
|
|
85
|
+
|
|
86
|
+
```rb
|
|
87
|
+
user system total real
|
|
88
|
+
map 36.110000 61.200000 97.310000 ( 99.535375)
|
|
89
|
+
select + map 10.530000 0.660000 11.190000 ( 12.550974)
|
|
90
|
+
select + as_json 49.040000 1.120000 50.160000 ( 55.417534)
|
|
91
|
+
pluck_all 3.310000 0.100000 3.410000 ( 3.527775)
|
|
92
|
+
```
|
|
93
|
+
Test by `benchmark-ips` and `limit 100` in each iteration:
|
|
94
|
+
```
|
|
95
|
+
Warming up --------------------------------------
|
|
96
|
+
map 1.000 i/100ms
|
|
97
|
+
select + map 28.000 i/100ms
|
|
98
|
+
select + as_json 7.000 i/100ms
|
|
99
|
+
pluck_all 54.000 i/100ms
|
|
100
|
+
Calculating -------------------------------------
|
|
101
|
+
map 14.230 (± 0.0%) i/s - 72.000 in 5.065349s
|
|
102
|
+
select + map 281.638 (± 4.6%) i/s - 1.428k in 5.081216s
|
|
103
|
+
select + as_json 73.241 (± 4.1%) i/s - 371.000 in 5.076235s
|
|
104
|
+
pluck_all 539.057 (± 6.7%) i/s - 2.700k in 5.034858s
|
|
105
|
+
|
|
106
|
+
Comparison:
|
|
107
|
+
pluck_all: 539.1 i/s
|
|
108
|
+
select + map: 281.6 i/s - 1.91x slower
|
|
109
|
+
select + as_json: 73.2 i/s - 7.36x slower
|
|
110
|
+
map: 14.2 i/s - 37.88x slower
|
|
111
|
+
```
|
|
112
|
+
[test script](https://github.com/khiav223577/pluck_all/issues/18)
|
|
113
|
+
|
|
114
|
+
### Compare with [pluck_to_hash](https://github.com/girishso/pluck_to_hash) gem
|
|
115
|
+
|
|
116
|
+
`pluck_all` has better performace since it uses raw `hash` data from `ActiveRecord::Base.connection.select_all`, while `pluck_to_hash` uses `pluck` method, which calls `ActiveRecord::Base.connection.select_all` and transfers the raw `hash` data to `array` format, and then transfer the data to `hash` format again. The following benchmark shows the performance difference:
|
|
117
|
+
|
|
118
|
+
```rb
|
|
119
|
+
user system total real
|
|
120
|
+
pluck_to_hash 2.960000 0.130000 3.090000 ( 3.421640)
|
|
121
|
+
pluck_all 2.160000 0.120000 2.280000 ( 2.605118)
|
|
122
|
+
```
|
|
123
|
+
Tested by `benchmark-ips` and `limit 1000` in each iteration:
|
|
124
|
+
```
|
|
125
|
+
Warming up --------------------------------------
|
|
126
|
+
pluck_to_hash 7.000 i/100ms
|
|
127
|
+
pluck_all 9.000 i/100ms
|
|
128
|
+
Calculating -------------------------------------
|
|
129
|
+
pluck_to_hash 84.526 (± 4.7%) i/s - 427.000 in 5.065792s
|
|
130
|
+
pluck_all 95.133 (± 4.2%) i/s - 477.000 in 5.021555s
|
|
131
|
+
|
|
132
|
+
Comparison:
|
|
133
|
+
pluck_all: 95.1 i/s
|
|
134
|
+
pluck_to_hash: 84.5 i/s - 1.13x slower
|
|
135
|
+
```
|
|
136
|
+
See the [test script](https://github.com/khiav223577/pluck_all/issues/18#issuecomment-325407080) for more details.
|
|
137
|
+
|
|
138
|
+
## Other Support
|
|
139
|
+
|
|
140
|
+
### Support globalize gem
|
|
141
|
+
|
|
142
|
+
```rb
|
|
143
|
+
class Post < ActiveRecord::Base
|
|
144
|
+
translates :title
|
|
145
|
+
end
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```rb
|
|
149
|
+
I18n.locale = :en
|
|
150
|
+
Post.pluck_all(:title)
|
|
151
|
+
# => [{ 'title' => 'english' }, { 'title' => 'english' }, ...]
|
|
152
|
+
|
|
153
|
+
I18n.locale = :'zh-TW'
|
|
154
|
+
Post.pluck_all(:title)
|
|
155
|
+
# => [{ 'title' => '中文' }, { 'title' => '中文' }, ...]
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
### Support Pluck Carrierwave Uploader (if you use carrierwave)
|
|
160
|
+
```rb
|
|
161
|
+
User.where(xxx).pluck_all(:profile_pic).map{|s| s['profile_pic'] }
|
|
162
|
+
```
|
|
163
|
+
is the same as
|
|
164
|
+
```rb
|
|
165
|
+
User.where(xxx).map(&:profile_pic)
|
|
166
|
+
```
|
|
167
|
+
If the uploader use something like: `model.id`, `model.name`
|
|
168
|
+
You may have to send these columns manually:
|
|
169
|
+
```rb
|
|
170
|
+
User.where(xxx).cast_need_columns(%i[id name]).pluck_all(:id, :name, :profile_pic).map{|s| s['profile_pic'] }
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Development
|
|
174
|
+
|
|
175
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test_active_record` or `rake test_mongoid` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
176
|
+
|
|
177
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
178
|
+
|
|
179
|
+
## Contributing
|
|
180
|
+
|
|
181
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/khiav223577/pluck_all. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
187
|
+
|
data/Rakefile
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
require 'bundler/gem_tasks'
|
|
2
|
-
require 'rake/testtask'
|
|
3
|
-
|
|
4
|
-
Rake::TestTask.new(:test) do |t|
|
|
5
|
-
t.libs << 'test'
|
|
6
|
-
t.libs << 'lib'
|
|
7
|
-
t.test_files = FileList['test/**/*_test.rb']
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
Rake::TestTask.new(:test_active_record) do |t|
|
|
11
|
-
t.libs << 'test'
|
|
12
|
-
t.libs << 'lib'
|
|
13
|
-
t.test_files = FileList['test/active_record/**/*_test.rb']
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
Rake::TestTask.new(:test_mongoid) do |t|
|
|
17
|
-
t.libs << 'test'
|
|
18
|
-
t.libs << 'lib'
|
|
19
|
-
t.test_files = FileList['test/mongoid/**/*_test.rb']
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
task default: :test
|
|
1
|
+
require 'bundler/gem_tasks'
|
|
2
|
+
require 'rake/testtask'
|
|
3
|
+
|
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
|
5
|
+
t.libs << 'test'
|
|
6
|
+
t.libs << 'lib'
|
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
Rake::TestTask.new(:test_active_record) do |t|
|
|
11
|
+
t.libs << 'test'
|
|
12
|
+
t.libs << 'lib'
|
|
13
|
+
t.test_files = FileList['test/active_record/**/*_test.rb']
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
Rake::TestTask.new(:test_mongoid) do |t|
|
|
17
|
+
t.libs << 'test'
|
|
18
|
+
t.libs << 'lib'
|
|
19
|
+
t.test_files = FileList['test/mongoid/**/*_test.rb']
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
task default: :test
|
data/bin/console
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
require 'bundler/setup'
|
|
4
|
-
require 'pluck_all'
|
|
5
|
-
|
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
-
|
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
-
# require "pry"
|
|
11
|
-
# Pry.start
|
|
12
|
-
|
|
13
|
-
require 'irb'
|
|
14
|
-
IRB.start
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
require 'pluck_all'
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require 'irb'
|
|
14
|
+
IRB.start
|
data/bin/setup
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
set -euo pipefail
|
|
3
|
-
IFS=$'\n\t'
|
|
4
|
-
set -vx
|
|
5
|
-
|
|
6
|
-
bundle install --gemfile=gemfiles/active_record_42.gemfile
|
|
7
|
-
|
|
8
|
-
# Do any other automated setup that you need to do here
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
IFS=$'\n\t'
|
|
4
|
+
set -vx
|
|
5
|
+
|
|
6
|
+
bundle install --gemfile=gemfiles/active_record_42.gemfile
|
|
7
|
+
|
|
8
|
+
# Do any other automated setup that you need to do here
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
source 'https://rubygems.org'
|
|
2
|
-
|
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
|
4
|
-
|
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
|
6
|
-
gem 'activerecord', '~> 3.2.0'
|
|
7
|
-
gem '
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
source 'https://rubygems.org'
|
|
2
|
+
|
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
|
4
|
+
|
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
|
6
|
+
gem 'activerecord', '~> 3.2.0'
|
|
7
|
+
gem 'rails_compatibility', '~> 0.0.7'
|
|
8
|
+
|
|
9
|
+
gem 'carrierwave', '~> 0.11.0'
|
|
10
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
|
11
|
+
|
|
12
|
+
group :test do
|
|
13
|
+
gem 'simplecov', '< 0.18'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
gemspec path: '../'
|