pluckex 1.0.1 → 1.1.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 +4 -4
- data/README.md +8 -2
- data/lib/pluckex/calculations.rb +16 -5
- data/lib/pluckex/version.rb +1 -1
- data/spec/lib/pluckex_spec.rb +9 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24fd35372b3fb4ba4fd10104485974766ccbb9f7
|
4
|
+
data.tar.gz: 3395c36f43859c48ab8747d9c87a1f89993aeea1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d7ad905d0adc041054dc0eafeacc8749cd8802340ad1124987423ae5d695e994eaa581d2db2584e7e0513850a38a4801a6445292ab96e7937394d5a3b1743c7
|
7
|
+
data.tar.gz: 965c8ae6f0b90f8d7dcefb5b7913236471532b13744e810a7feb5826d621411cbc8bd42fc0496e89f3286260411bd70d32a3d85fe291c1f853d0aa54e3f8dc62
|
data/README.md
CHANGED
@@ -9,8 +9,14 @@ Like this.
|
|
9
9
|
### pluck_tied_by_id
|
10
10
|
|
11
11
|
```
|
12
|
-
|
13
|
-
|
12
|
+
# one arg case:
|
13
|
+
# Person.pluck_tied_by_id(:name)
|
14
|
+
# # => {1=>'David', 2=>'Jeremy', 3=>'Jose'}
|
15
|
+
#
|
16
|
+
# multiple args case:
|
17
|
+
# Person.pluck_tied_by_id(:name, :age)
|
18
|
+
# # => {1=>{:name=>"David", :age=>25}, 2=>{:name=>"Jeremy", :age=>27}}
|
19
|
+
|
14
20
|
```
|
15
21
|
### pluck_with_keys
|
16
22
|
|
data/lib/pluckex/calculations.rb
CHANGED
@@ -1,13 +1,24 @@
|
|
1
1
|
module Pluckex
|
2
2
|
module Calculations
|
3
|
+
# one arg case:
|
3
4
|
# Person.pluck_tied_by_id(:name)
|
4
|
-
#
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
# # => {1=>'David', 2=>'Jeremy', 3=>'Jose'}
|
6
|
+
#
|
7
|
+
# multiple args case:
|
8
|
+
# Person.pluck_tied_by_id(:name, :age)
|
9
|
+
# # => {1=>{:name=>"David", :age=>25}, 2=>{:name=>"Jeremy", :age=>27}}
|
10
|
+
def pluck_tied_by_id(*col_name)
|
11
|
+
if col_name.length == 1
|
12
|
+
col_names = [:id] << col_name.first
|
13
|
+
Hash[*self.pluck(*col_names).flatten]
|
14
|
+
else
|
15
|
+
col_hash_array = self.pluck_with_keys(*col_name)
|
16
|
+
id_array = self.pluck(:id)
|
17
|
+
Hash[*[id_array, col_hash_array].transpose.flatten]
|
18
|
+
end
|
8
19
|
end
|
9
20
|
|
10
|
-
#
|
21
|
+
# Person.pluck_with_keys(:id, :name)
|
11
22
|
# # => [{:id=>1, :name=>'David'}, {:id=>2, :name=>'Jeremy'}, {:id=>3, :name=>'Jose'}]
|
12
23
|
def pluck_with_keys(*col_names)
|
13
24
|
self.pluck(*col_names).map do |element|
|
data/lib/pluckex/version.rb
CHANGED
data/spec/lib/pluckex_spec.rb
CHANGED
@@ -12,6 +12,15 @@ describe Pluckex do
|
|
12
12
|
)
|
13
13
|
end
|
14
14
|
end
|
15
|
+
describe 'pluck_tied_by_id' do
|
16
|
+
subject { Person.pluck_tied_by_id(:name, :age) }
|
17
|
+
it do
|
18
|
+
expect(subject).to eq(
|
19
|
+
people.first.id => { :name => people.first.name, :age => people.first.age },
|
20
|
+
people.second.id =>{ :name => people.second.name, :age => people.second.age },
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
15
24
|
describe 'pluck_with_keys' do
|
16
25
|
subject { Person.pluck_with_keys(:id, :name) }
|
17
26
|
it do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluckex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daiki Taniguchi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|