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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a620491959579188c8be6b6bf1897677bc6b87d
4
- data.tar.gz: 5d9df383e03292335d95bc3c714ac2478627d669
3
+ metadata.gz: 24fd35372b3fb4ba4fd10104485974766ccbb9f7
4
+ data.tar.gz: 3395c36f43859c48ab8747d9c87a1f89993aeea1
5
5
  SHA512:
6
- metadata.gz: f63e4122c8450b1bbf0f746df38daad4b65d821c77fc89590e0632772246c7cac9919e45a0dc5e971cdd3ed5ed8376f8534461c7f235192c81865f4ba90cb240
7
- data.tar.gz: 50da4b23e5f366599684167b0eed1ce6a634007c23415cd335977ba35c2345fed22b397a142ed43645084b93a77a2c32d37a0622f8cdbd500e09ca8b950f1371
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
- # Person.pluck_tied_by_id(:name)
13
- # # => {1=>'David', 2=>'Jeremy', 3=>'Jose'}
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
 
@@ -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
- # # => {1=>'David', 2=>'Jeremy', 3=>'Jose'}
5
- def pluck_tied_by_id(col_name)
6
- col_names = [:id] << col_name
7
- Hash[*self.pluck(*col_names).flatten]
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
- # Person.pluck_with_keys(:id, :name)
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|
@@ -1,3 +1,3 @@
1
1
  module Pluckex
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.1"
3
3
  end
@@ -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.0.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-15 00:00:00.000000000 Z
11
+ date: 2014-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler