deep_pluck 1.1.0 → 1.1.1

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
- SHA1:
3
- metadata.gz: 19d462353caa430573529609f5761ee472a5f19d
4
- data.tar.gz: 05d5b136c5018928378669da37fc7eddd8ce3686
2
+ SHA256:
3
+ metadata.gz: f546a72a6ba6196d025dab3228d9f7bdb96ceed1675c10d4bdb04f9fd7cbd047
4
+ data.tar.gz: 99b48a384f25ccf25b7996d9cfcfc8594a4dd9bbc41e71976481ede3e4b89e94
5
5
  SHA512:
6
- metadata.gz: af0a2b2cec4422dc4471c7e8a08057889c042c18d246f923f8d8864a34c8816fb58b02359790aa536acf9c0a2887bf4a14f6fce262ecd17d3850602d661fa611
7
- data.tar.gz: 64acd5ce3393d4d7d9d2055266826ab9e06e4888a8f4f84d558b7299f2dc8f8ece40df57491e81c392da1bb05eff219d74ed86ec41ffff7eb2e5db8ab3334910
6
+ metadata.gz: 326f4d9c3af855a85e48935ddc25e26e59603a3b43fe1c5b5e7e5dcf7450dcbf33dea3208215c19c7da60d91e6b570d66cb266de7a4d84de2434b6412d09e830
7
+ data.tar.gz: 9239f528f10b466c00ce367da8d8a5eb546264627e179c37a80ad492d00a80cec3f2ae0168955c14a4625ea1d0a798daa9be24d9aab1a613f739c502ce08c28f
@@ -13,6 +13,7 @@ gemfile:
13
13
  - gemfiles/4.2.gemfile
14
14
  - gemfiles/5.0.gemfile
15
15
  - gemfiles/5.1.gemfile
16
+ - gemfiles/5.2.gemfile
16
17
  addons:
17
18
  code_climate:
18
19
  repo_token: 29e69e7da6b55678f27b7de2444b6161a6c98e3c4b22e0e017ebab87ada18fd5
@@ -0,0 +1,43 @@
1
+ ## Change Log
2
+
3
+ ### [upcoming](https://github.com/khiav223577/deep_pluck/compare/v1.1.0...HEAD) 2018/04/05
4
+ - [#22](https://github.com/khiav223577/deep_pluck/pull/22) #deep_pluck at active model without plucking deeply will cause ArgumentError (@khiav223577)
5
+ - [#20](https://github.com/khiav223577/deep_pluck/pull/20) [ENHANCE] Eliminate Extra Select Loop in Hash Lookup (@berniechiu)
6
+
7
+ ### [v1.1.0](https://github.com/khiav223577/deep_pluck/compare/v1.0.3...v1.1.0) 2018/02/15
8
+ - [#19](https://github.com/khiav223577/deep_pluck/pull/19) Support deep_pluck at active model (@khiav223577)
9
+ - [#18](https://github.com/khiav223577/deep_pluck/pull/18) Add rubocop and Improve code quality (@khiav223577)
10
+
11
+ ### [v1.0.3](https://github.com/khiav223577/deep_pluck/compare/v1.0.0...v1.0.3) 2017/06/30
12
+ - [#15](https://github.com/khiav223577/deep_pluck/pull/15) test deep_pluck in rails 5.1.x (@khiav223577)
13
+ - [#14](https://github.com/khiav223577/deep_pluck/pull/14) Handle polymorphic associations correctly. (@Bogadon)
14
+
15
+ ### [v1.0.0](https://github.com/khiav223577/deep_pluck/compare/v0.1.4...v1.0.0) 2017/03/28
16
+ - [#12](https://github.com/khiav223577/deep_pluck/pull/12) reduce cyclomatic complexity in model.rb (@khiav223577)
17
+
18
+ ### [v0.1.4](https://github.com/khiav223577/deep_pluck/compare/v0.1.3...v0.1.4) 2017/03/27
19
+ - [#11](https://github.com/khiav223577/deep_pluck/pull/11) Fix conditional associations (@khiav223577)
20
+
21
+ ### [v0.1.3](https://github.com/khiav223577/deep_pluck/compare/v0.1.2...v0.1.3) 2017/03/20
22
+ - [#10](https://github.com/khiav223577/deep_pluck/pull/10) fix custom foreign_key, custom primary_key issues (@khiav223577)
23
+ - [#9](https://github.com/khiav223577/deep_pluck/pull/9) fix has_and_belongs_to_many (@khiav223577)
24
+
25
+ ### [v0.1.2](https://github.com/khiav223577/deep_pluck/compare/v0.1.1...v0.1.2) 2017/03/17
26
+ - [#8](https://github.com/khiav223577/deep_pluck/pull/8) fix some need columns is missing (@khiav223577)
27
+ - [#7](https://github.com/khiav223577/deep_pluck/pull/7) raise error message when association not found (@khiav223577)
28
+
29
+ ### [v0.1.1](https://github.com/khiav223577/deep_pluck/compare/v0.1.0...v0.1.1) 2017/03/16
30
+ - [#6](https://github.com/khiav223577/deep_pluck/pull/6) fix deep_pluck with #joins (@khiav223577)
31
+
32
+ ### [v0.1.0](https://github.com/khiav223577/deep_pluck/compare/v0.0.4...v0.1.0) 2017/03/15
33
+ - [#5](https://github.com/khiav223577/deep_pluck/pull/5) Support pluck many-to-many associations (@khiav223577)
34
+
35
+ ### [v0.0.4](https://github.com/khiav223577/deep_pluck/compare/v0.0.3...v0.0.4) 2017/03/14
36
+ - [#4](https://github.com/khiav223577/deep_pluck/pull/4) Fix use deep_pluck on NullRelation will raise exception and prevent unneeded query. (@khiav223577)
37
+
38
+ ### [v0.0.3](https://github.com/khiav223577/deep_pluck/compare/v0.0.2...v0.0.3) 2017/03/06
39
+ - [#3](https://github.com/khiav223577/deep_pluck/pull/3) support more than two level (@khiav223577)
40
+
41
+ ### v0.0.2 2017/03/06
42
+ - [#2](https://github.com/khiav223577/deep_pluck/pull/2) The result of has_one association should not be array (@khiav223577)
43
+ - [#1](https://github.com/khiav223577/deep_pluck/pull/1) deep pluck (@khiav223577)
data/README.md CHANGED
@@ -8,9 +8,7 @@
8
8
 
9
9
  Allow you to pluck deeply into nested associations without loading a bunch of records.
10
10
 
11
- And DRY up your code when using #as_json.
12
-
13
- Works in Rails 3+.
11
+ Support Rails 3.2, 4.2, 5.0, 5.1, 5.2.
14
12
 
15
13
 
16
14
  ## Installation
@@ -32,29 +30,57 @@ Or install it yourself as:
32
30
  ## Usage
33
31
 
34
32
  ### Similar to #pluck method
33
+
35
34
  ```rb
36
35
  User.deep_pluck(:id, :name)
37
36
  # SELECT `users`.`id`, `users`.`name` FROM `users`
38
- # => [{'id' => 1, 'name' => 'David'}, {'id' => 2, 'name' => 'Jeremy'}]
37
+ # =>
38
+ # [
39
+ # {'id' => 1, 'name' => 'David'},
40
+ # {'id' => 2, 'name' => 'Jeremy'},
41
+ # ]
39
42
  ```
40
43
 
41
- ### Pluck deep into associations
44
+ ### Pluck attributes from nested associations
45
+
42
46
  ```rb
43
- User.deep_pluck(:name, :posts => :title)
47
+ User.deep_pluck(:name, 'posts' => :title)
44
48
  # SELECT `users`.`id`, `users`.`name` FROM `users`
45
49
  # SELECT `posts`.`user_id`, `posts`.`title` FROM `posts` WHERE `posts`.`user_id` IN (1, 2)
46
- # => [
47
- # {'name' => 'David' , :posts => [{'title' => 'post1'}, {'title' => 'post2'}]},
48
- # {'name' => 'Jeremy', :posts => [{'title' => 'post3'}]}
50
+ # =>
51
+ # [
52
+ # {
53
+ # 'name' => 'David' ,
54
+ # 'posts' => [
55
+ # {'title' => 'post1'},
56
+ # {'title' => 'post2'},
57
+ # ],
58
+ # },
59
+ # {
60
+ # 'name' => 'Jeremy',
61
+ # 'posts' => [
62
+ # {'title' => 'post3'},
63
+ # ],
64
+ # },
49
65
  # ]
50
66
  ```
51
- ### Support plucking at active models
67
+
68
+ ### Pluck at models
69
+
52
70
  ```rb
53
- user = User.find_by(name: 'David').deep_pluck(:name, :posts => :title)
54
- # => {'name' => 'David' , :posts => [{'title' => 'post1'}, {'title' => 'post2'}]}
71
+ user = User.find_by(name: 'David')
72
+ user.deep_pluck(:name, :posts => :title)
73
+ # =>
74
+ # {
75
+ # 'name' => 'David' ,
76
+ # :posts => [
77
+ # {'title' => 'post1'},
78
+ # {'title' => 'post2'},
79
+ # ],
80
+ # }
55
81
  ```
56
82
 
57
- ### DRY up Rails/ActiveRecord includes when using as_json
83
+ ### Compare with using `#as_json`
58
84
 
59
85
  Assume the following relations:
60
86
 
@@ -85,7 +111,9 @@ User.where(:name => %w(Pearl Kathenrie)).includes([{:posts => :post_comments}, :
85
111
  ```
86
112
  It works as expected, but is not very DRY, repeat writing `include`, `posts`, `post_comments` so many times.
87
113
 
88
- You could refactor it with #deep_pluck like:
114
+ Not to mention the huge performace improvement by using #deep_pluck.
115
+
116
+ You could refactor the example with #deep_pluck:
89
117
  ```rb
90
118
  User.where(:name => %w(Pearl Kathenrie)).deep_pluck(
91
119
  :name,
@@ -97,9 +125,7 @@ User.where(:name => %w(Pearl Kathenrie)).deep_pluck(
97
125
 
98
126
  ### Better Performance
99
127
 
100
- #deep_pluck return raw hash data without loading a bunch of records.
101
-
102
- In that faster than #as_json, or #select.
128
+ #deep_pluck return raw hash data without loading a bunch of records, so that faster than #as_json, or #select.
103
129
 
104
130
  The following is the benchmark test on 3 users, 6 posts, where `users` table have 14 columns and `posts` have 6 columns. As it shows, `deep_pluck` is 4x faster than `as_json`.
105
131
 
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in pluck_all.gemspec
4
4
 
5
- gem "activerecord", "~> 3.2"
5
+ gem "activerecord", "~> 3.2.0"
6
6
  gem "pluck_all", "~> 1.2.2"
7
7
 
8
8
  group :test do
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in pluck_all.gemspec
4
4
 
5
- gem "activerecord", "~> 4.2"
5
+ gem "activerecord", "~> 4.2.0"
6
6
  gem "pluck_all", "~> 1.2.2"
7
7
 
8
8
  group :test do
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in pluck_all.gemspec
4
4
 
5
- gem "activerecord", "~> 5.0"
5
+ gem "activerecord", "~> 5.0.0"
6
6
  gem "pluck_all", "~> 1.2.2"
7
7
 
8
8
  group :test do
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in pluck_all.gemspec
4
4
 
5
- gem "activerecord", "~> 5.1"
5
+ gem "activerecord", "~> 5.1.0"
6
6
  gem "pluck_all", "~> 1.2.2"
7
7
 
8
8
  group :test do
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pluck_all.gemspec
4
+
5
+ gem "activerecord", "~> 5.2.0"
6
+ gem "pluck_all", "~> 1.2.2"
7
+
8
+ group :test do
9
+ gem "simplecov"
10
+ gem "codeclimate-test-reporter", "~> 1.0.0"
11
+ end
12
+
13
+ gemspec :path => "../"
14
+
@@ -15,10 +15,10 @@ class ActiveRecord::Base
15
15
  end
16
16
 
17
17
  def deep_pluck(*args)
18
- hash_args = args.select{|s| s.is_a?(Hash) }
19
- other_args = args.select{|s| !s.is_a?(Hash) }
18
+ hash_args, other_args = args.partition { |s| s.is_a?(Hash) }
20
19
  preloaded_model = DeepPluck::PreloadedModel.new(self, other_args)
21
20
  model = DeepPluck::Model.new(self.class.where(id: id), preloaded_model: preloaded_model)
22
- return model.add(*hash_args).load_all.first
21
+ model.add(*hash_args) if hash_args.any?
22
+ return model.load_all.first
23
23
  end
24
24
  end
@@ -10,7 +10,7 @@ module DeepPluck
10
10
  @preloaded_model = preloaded_model
11
11
  @parent_association_key = parent_association_key
12
12
  @parent_model = parent_model
13
- @need_columns = []
13
+ @need_columns = (preloaded_model ? preloaded_model.need_columns : [])
14
14
  @associations = {}
15
15
  end
16
16
 
@@ -1,5 +1,7 @@
1
1
  module DeepPluck
2
2
  class PreloadedModel
3
+ attr_reader :need_columns
4
+
3
5
  def initialize(active_model, need_columns)
4
6
  @active_model = active_model
5
7
  @need_columns = need_columns
@@ -1,3 +1,3 @@
1
1
  module DeepPluck
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_pluck
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-15 00:00:00.000000000 Z
11
+ date: 2018-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -106,6 +106,7 @@ files:
106
106
  - ".gitignore"
107
107
  - ".rubocop.yml"
108
108
  - ".travis.yml"
109
+ - CHANGELOG.md
109
110
  - CODE_OF_CONDUCT.md
110
111
  - LICENSE.txt
111
112
  - README.md
@@ -117,6 +118,7 @@ files:
117
118
  - gemfiles/4.2.gemfile
118
119
  - gemfiles/5.0.gemfile
119
120
  - gemfiles/5.1.gemfile
121
+ - gemfiles/5.2.gemfile
120
122
  - lib/deep_pluck.rb
121
123
  - lib/deep_pluck/data_combiner.rb
122
124
  - lib/deep_pluck/model.rb
@@ -142,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
144
  version: '0'
143
145
  requirements: []
144
146
  rubyforge_project:
145
- rubygems_version: 2.6.13
147
+ rubygems_version: 2.7.6
146
148
  signing_key:
147
149
  specification_version: 4
148
150
  summary: Use deep_pluck as a shortcut to select one or more attributes and include