deep_pluck 0.1.1 → 0.1.2

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: 02455f382f53378da4401571796aa5f0d200327e
4
- data.tar.gz: a7ae7dcc3a2e5b38416766ae1ac0564574375660
3
+ metadata.gz: 51c4e3a56a22aaf35ded1d2614e87d76f5b52283
4
+ data.tar.gz: 5638b67af223f0c2c34009abdcfa7d7fe75c1186
5
5
  SHA512:
6
- metadata.gz: db336700568010031bf5a22f4eb98dcee584b5f631d2d74c78d251a811ac4f857f5b3b09093df3350b0d74732ecf19fa54c0b012de3fbd8a6503ef9c7f9f0573
7
- data.tar.gz: 9c273df567114544e2b356dbc67d80bf1839e41dd0617983bcde494ed98b8cbabf547057970997a58a5fc63b61b7db0bf7f9c0a9d8217260326f717aad7eb46d
6
+ metadata.gz: 76a4685fbe45190c0da0c75175dcc0769da3a43007be9791bf78f06208cf701534f997f946b72f095916cda4e8971bf1583de2758166a6ded366849d2c1dd814
7
+ data.tar.gz: 57c237f957442f8b04c915808f1f3e58491000e78ec0aae54a312dd8c2d9f051886269fcc6e323dfaa942c42b5cb8c12afe88a447405d4ba483f90e12fd99b2b
data/README.md CHANGED
@@ -27,6 +27,71 @@ Or install it yourself as:
27
27
 
28
28
  ## Usage
29
29
 
30
+ ### Similar to #pluck method
31
+ ```rb
32
+ User.deep_pluck(:id, :name)
33
+ # SELECT `users`.`id`, `users`.`name` FROM `users`
34
+ # => [{'id' => 1, 'name' => 'David'}, {'id' => 2, 'name' => 'Jeremy'}]
35
+ ```
36
+
37
+ ### Pluck deep into associations
38
+ ```rb
39
+ User.deep_pluck(:name, 'posts' => :title)
40
+ # SELECT `users`.`id`, `users`.`name` FROM `users`
41
+ # SELECT `posts`.`user_id`, `posts`.`title` FROM `posts` WHERE `posts`.`user_id` IN (1, 2)
42
+ # => [
43
+ # {'name' => 'David' , 'posts' => [{'title' => 'post1'}, {'title' => 'post2'}]},
44
+ # {'name' => 'Jeremy', 'posts' => [{'title' => 'post3'}]}
45
+ # ]
46
+ ```
47
+
48
+ ### DRY up Rails/ActiveRecord includes when using as_json
49
+
50
+ Assume the following relations:
51
+
52
+ > User has_many Posts.<br>
53
+ > Post has_many PostComments.<br>
54
+ > User has_one Contact.<br>
55
+
56
+ And the following #as_json example:
57
+ ```rb
58
+ User.where(:name => %w(Pearl Kathenrie)).includes([{:posts => :post_comments}, :contact]).as_json({
59
+ :root => false,
60
+ :only => [:name, :email],
61
+ :include => {
62
+ 'posts' => {
63
+ :only => :name,
64
+ :include => {
65
+ 'post_comments' => {
66
+ :only => :comment,
67
+ },
68
+ },
69
+ },
70
+ 'contact' => {
71
+ :only => :address,
72
+ },
73
+ },
74
+ })
75
+
76
+ ```
77
+ You could refactor it with #deep_pluck like:
78
+ ```rb
79
+ User.where(:name => %w(Pearl Kathenrie)).deep_pluck(
80
+ :name,
81
+ :email,
82
+ 'posts' => [:name, 'post_comments' => :comment],
83
+ 'contact' => :address,
84
+ )
85
+ ```
86
+
87
+ ### Better Performance
88
+
89
+ #deep_pluck return raw hash data without loading a bunch of records.
90
+
91
+ In that faster than #as_json, or #select.
92
+
93
+ Will add some benchmarks soon :)
94
+
30
95
 
31
96
  ## Development
32
97
 
@@ -14,7 +14,8 @@ module DeepPluck
14
14
  # Reader
15
15
  #---------------------------------------
16
16
  def get_reflect(association_key)
17
- @relation.klass.reflect_on_association(association_key.to_sym) #add to_sym since rails 3 only support symbol
17
+ @relation.klass.reflect_on_association(association_key.to_sym) || #add to_sym since rails 3 only support symbol
18
+ raise(ActiveRecord::ConfigurationError, "ActiveRecord::ConfigurationError: Association named '#{association_key}' was not found on #{@relation.klass.name}; perhaps you misspelled it?")
18
19
  end
19
20
  def get_foreign_key(reflect, reverse: false, with_table_name: false)
20
21
  if reflect.options[:through] and reverse #reverse = parent
@@ -104,12 +105,12 @@ module DeepPluck
104
105
  def load_data
105
106
  prev_need_columns = @parent_model.get_foreign_key(@parent_model.get_reflect(@parent_association_key), reverse: true, with_table_name: true) if @parent_model
106
107
  next_need_columns = @associations.map{|key, _| get_foreign_key(get_reflect(key), with_table_name: true) }.uniq
107
- all_need_columns = [*prev_need_columns, *next_need_columns, *@need_columns].uniq
108
+ all_need_columns = [*prev_need_columns, *next_need_columns, *@need_columns].uniq(&Helper::TO_KEY_PROC)
108
109
  @relation = yield(@relation) if block_given?
109
110
  @data = @relation.pluck_all(*all_need_columns)
110
111
  if @data.size != 0
111
- @extra_columns = all_need_columns - @need_columns #for delete_extra_column_data!
112
- @extra_columns.map!{|s| s.gsub(/\w*[^\w]/, '')} #user_achievements.user_id => user_id
112
+ #for delete_extra_column_data!
113
+ @extra_columns = all_need_columns.map(&Helper::TO_KEY_PROC) - @need_columns.map(&Helper::TO_KEY_PROC)
113
114
  @associations.each do |key, model|
114
115
  set_includes_data(@data, key, model)
115
116
  end
@@ -126,5 +127,16 @@ module DeepPluck
126
127
  @data.each{|s| s.except!(*@extra_columns) }
127
128
  @associations.each{|_, model| model.delete_extra_column_data! }
128
129
  end
130
+ #---------------------------------------
131
+ # Helper methods
132
+ #---------------------------------------
133
+ module Helper
134
+ TO_KEY_PROC = proc{|s| Helper.column_to_key(s) }
135
+ def self.column_to_key(key) #user_achievements.user_id => user_id
136
+ key = key[/(\w+)[^\w]*\z/]
137
+ key.gsub!(/[^\w]+/, '')
138
+ return key
139
+ end
140
+ end
129
141
  end
130
142
  end
@@ -1,3 +1,3 @@
1
1
  module DeepPluck
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
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: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-16 00:00:00.000000000 Z
11
+ date: 2017-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  version: '0'
139
139
  requirements: []
140
140
  rubyforge_project:
141
- rubygems_version: 2.4.8
141
+ rubygems_version: 2.6.8
142
142
  signing_key:
143
143
  specification_version: 4
144
144
  summary: Use deep_pluck as a shortcut to select one or more attributes and include