looksist 0.3.6 → 0.3.7

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: 917b96f2edca2099bafd294b7e2eee0df32bb0c7
4
- data.tar.gz: b1de0ae1e63f3035a87963d689b7368e5092effd
3
+ metadata.gz: 35a51afb4989fbb14f003fa155e50e14afb90439
4
+ data.tar.gz: 4295b2ff68f81062ab5d2911302de3998a0d22f2
5
5
  SHA512:
6
- metadata.gz: 715905bf10eac5ba3961d3834692663014dc58cc5f5dc569aec580a265424b0069a53712b0b65a404f895e92b7c202b2723b3482a16d3d39bbbd490591be9722
7
- data.tar.gz: cdda079c645c63cc019c46461e96b61d8f27bea4c2feb6745a95540906551f9abad14664f51cf60b9e1c12fa41948f90d3eebd64c83d6b631b3c2eace957d0fd
6
+ metadata.gz: 179e3288026145a68f0548aed4e284d104b92354db7583cba9f64b463d627094efacb2cf8ae73d6f865974a4c78dbfccf68aae00ff510203fcc445a7ee188308
7
+ data.tar.gz: a664389618be8eb5f89032d69ba1ddfd422b2c3868b13572f3b6b1be8388e8315709927adb1dc9c072e8c47e69171517ee1d8337f422e77d5e7fcda751c0f8d7
@@ -26,11 +26,12 @@ module Looksist
26
26
  def inject_instance_methods(after)
27
27
  define_method("#{after}_with_inject") do |*args|
28
28
  hash = send("#{after}_without_inject".to_sym, *args)
29
- self.class.instance_variable_get(:@rules)[after].each do |opts|
30
- if opts[:at].nil? or opts[:at].is_a? String
31
- hash = self.class.update_using_json_path(hash, opts)
29
+ rules_group_by_at = self.class.instance_variable_get(:@rules)[after].group_by { |e| e[:at] }
30
+ rules_group_by_at.each do |at, opts|
31
+ if at.nil? or at.is_a? String
32
+ hash = self.class.update_using_json_path(hash, at, opts)
32
33
  else
33
- self.class.inject_attributes_at(hash[opts[:at]], opts)
34
+ self.class.inject_attributes_at(hash[at], opts)
34
35
  end
35
36
  end
36
37
  hash
@@ -41,11 +42,12 @@ module Looksist
41
42
  def inject_class_methods(after)
42
43
  define_singleton_method("#{after}_with_inject") do |*args|
43
44
  hash = send("#{after}_without_inject".to_sym, *args)
44
- @rules[after].each do |opts|
45
- if opts[:at].nil? or opts[:at].is_a? String
46
- hash = update_using_json_path(hash, opts)
45
+ rules_group_by_at = @rules[after].group_by { |e| e[:at] }
46
+ rules_group_by_at.each do |at, opts|
47
+ if at.nil? or at.is_a? String
48
+ hash = update_using_json_path(hash, at, opts)
47
49
  else
48
- inject_attributes_at(hash[opts[:at]], opts)
50
+ inject_attributes_at(hash[at], opts)
49
51
  end
50
52
  end
51
53
  hash
@@ -55,118 +57,111 @@ module Looksist
55
57
 
56
58
  def inject_attributes_at(hash_offset, opts)
57
59
  return hash_offset if hash_offset.nil? or hash_offset.empty?
58
- keys = hash_offset[opts[:using]]
59
- entity_name = __entity__(opts[:bucket_name] || opts[:using])
60
- values = Looksist.redis_service.send("#{entity_name}_for", keys)
61
- if opts[:populate].is_a? Array
62
- opts[:populate].each do |elt|
63
- value_hash = values.each_with_object([]) do |i, acc|
64
- acc << JSON.parse(i || '{}').deep_symbolize_keys[elt]
60
+ opts.each do |opt|
61
+ keys = hash_offset[opt[:using]]
62
+ entity_name = __entity__(opt[:bucket_name] || opt[:using])
63
+ values = Looksist.redis_service.send("#{entity_name}_for", keys)
64
+ if opt[:populate].is_a? Array
65
+ opt[:populate].each do |elt|
66
+ value_hash = values.each_with_object([]) do |i, acc|
67
+ acc << JSON.parse(i || '{}').deep_symbolize_keys[elt]
68
+ end
69
+ alias_method = find_alias(opt[:as], elt)
70
+ hash_offset[alias_method] = value_hash
65
71
  end
66
- alias_method = find_alias(opts[:as], elt)
67
- hash_offset[alias_method] = value_hash
72
+ else
73
+ alias_method = find_alias(opt[:as], opt[:populate])
74
+ hash_offset[alias_method] = values
68
75
  end
69
- else
70
- alias_method = find_alias(opts[:as], opts[:populate])
71
- hash_offset[alias_method] = values
72
- hash_offset
73
76
  end
77
+ hash_offset
74
78
  end
75
79
 
76
- def update_using_json_path(hash, opts)
80
+ def update_using_json_path(hash, at, opts)
77
81
  if hash.is_a?(Hash)
78
- if opts[:at].present?
79
- JsonPath.for(hash.with_indifferent_access).gsub!(opts[:at]) do |i|
80
- i.is_a?(Array) ? inject_attributes_for(i, opts) : inject_attributes_at(i, opts) unless (i.nil? or i.empty?)
82
+ if at.present?
83
+ JsonPath.for(hash.with_indifferent_access).gsub!(at) do |i|
84
+ i.is_a?(Array) ? inject_attributes_for(i, at, opts) : inject_attributes_at(i, opts) unless (i.nil? or i.empty?)
81
85
  i
82
86
  end
83
87
  else
84
88
  inject_attributes_at(hash, opts)
85
-
86
89
  end.to_hash.deep_symbolize_keys
87
90
  else
88
- inject_attributes_for_array(hash, opts)
91
+ inject_attributes_for_array(hash, at, opts)
89
92
  end
90
93
  end
91
94
 
92
- def inject_attributes_for(array_of_hashes, opts)
93
- entity_name = __entity__(opts[:bucket_name] || opts[:using])
94
- keys = (array_of_hashes.collect { |i| i[opts[:using]] }).compact.uniq
95
- values = Hash[keys.zip(Looksist.redis_service.send("#{entity_name}_for", keys))]
96
- opts[:populate].is_a?(Array) ? composite_attribute_lookup(array_of_hashes, opts, values) : single_attribute_lookup(array_of_hashes, opts, values)
97
- end
98
-
99
- def inject_attributes_for_array(array_of_hashes, opts)
100
- entity_name = __entity__(opts[:bucket_name] || opts[:using])
101
- modified_array = if opts[:at].nil?
102
- array_of_hashes.map(&:values)
103
- else
104
- json_path = JsonPath.new("#{opts[:at]}..#{opts[:using]}")
105
- json_path.on(array_of_hashes.to_json)
106
- end
107
- keys = modified_array.flatten.compact.uniq
108
- values = Hash[keys.zip(Looksist.redis_service.send("#{entity_name}_for", keys))]
109
- opts[:populate].is_a?(Array) ? composite_attribute_lookup(array_of_hashes, opts, values) : single_attribute_lookup_for_array(array_of_hashes, opts, values)
110
- end
111
-
112
- def single_attribute_lookup(array_of_hashes, opts, values)
113
- array_of_hashes.each do |elt|
114
- alias_method = find_alias(opts[:as], opts[:populate])
115
- elt[alias_method] = values[elt[opts[:using]]]
95
+ def inject_attributes_for(array_of_hashes, at, opts)
96
+ all_values = opts.each_with_object({}) do |opt, acc|
97
+ entity_name = __entity__(opt[:bucket_name] || opt[:using])
98
+ keys = (array_of_hashes.collect { |i| i[opt[:using]] }).compact.uniq
99
+ values = Hash[keys.zip(Looksist.redis_service.send("#{entity_name}_for", keys))]
100
+ acc[opt[:using]] = values
116
101
  end
102
+ smart_lookup(array_of_hashes, opts, all_values, nil)
117
103
  end
118
104
 
119
- def single_attribute_lookup_for_array(array_of_hashes, opts, values)
120
- array_of_hashes.collect do |elt|
121
- alias_method = find_alias(opts[:as], opts[:populate])
122
- if opts[:at].present?
123
- JsonPath.for(elt.with_indifferent_access).gsub!(opts[:at]) do |node|
124
- if node.is_a? Array
125
- node.each { |x| x[alias_method] = values[x.with_indifferent_access[opts[:using]]] }
126
- else
127
- node[alias_method] = values[node.with_indifferent_access[opts[:using]]]
128
- end
129
- node
130
- end.to_hash.deep_symbolize_keys
131
- else
132
- elt[alias_method] = values[elt[opts[:using]]]
133
- elt
134
- end
105
+ def inject_attributes_for_array(array_of_hashes, at, opts)
106
+ all_values = opts.each_with_object({}) do |opt, acc|
107
+ entity_name = __entity__(opt[:bucket_name] || opt[:using])
108
+ modified_array = if at.nil?
109
+ array_of_hashes.map(&:values)
110
+ else
111
+ json_path = JsonPath.new("#{at}..#{opt[:using]}")
112
+ json_path.on(array_of_hashes.to_json)
113
+ end
114
+ keys = modified_array.flatten.compact.uniq
115
+ values = Hash[keys.zip(Looksist.redis_service.send("#{entity_name}_for", keys))]
116
+ acc[opt[:using]] = values
135
117
  end
118
+ smart_lookup(array_of_hashes, opts, all_values, at)
136
119
  end
137
120
 
138
- def composite_attribute_lookup(array_of_hashes, opts, values)
121
+ def smart_lookup(array_of_hashes, opts, all_values, at)
122
+ ## populate is not a array
139
123
  array_of_hashes.collect do |elt|
140
- if opts[:at].present?
141
- JsonPath.for(elt.with_indifferent_access).gsub!(opts[:at]) do |node|
124
+ if at.present?
125
+ JsonPath.for(elt.with_indifferent_access).gsub!(at) do |node|
142
126
  if node.is_a? Array
143
- node.collect do |x|
144
- opts[:populate].collect do |_key|
145
- alias_method = find_alias(opts[:as], _key)
146
- parsed_key = JSON.parse(values[x.with_indifferent_access[opts[:using]]]).deep_symbolize_keys
147
- x[alias_method] = parsed_key[_key]
127
+ node.each do |x|
128
+ opts.each do |opt|
129
+ values = all_values[opt[:using]]
130
+ do_populate(x, values, opt[:using], opt[:as], opt[:populate])
148
131
  end
149
132
  end
150
133
  else
151
- parsed_key = JSON.parse(values[node.with_indifferent_access[opts[:using]]]).deep_symbolize_keys
152
- opts[:populate].collect do |_key|
153
- alias_method = find_alias(opts[:as], _key)
154
- node[alias_method] = parsed_key[_key]
134
+ opts.each do |opt|
135
+ values = all_values[opt[:using]]
136
+ do_populate(node, values, opt[:using], opt[:as], opt[:populate])
155
137
  end
156
138
  end
157
139
  node
158
140
  end.to_hash.deep_symbolize_keys
159
141
  else
160
- parsed_key = JSON.parse(values[elt[opts[:using]]]).deep_symbolize_keys
161
- opts[:populate].collect do |_key|
162
- alias_method = find_alias(opts[:as], _key)
163
- elt[alias_method] = parsed_key[_key]
142
+ opts.each do |opt|
143
+ values = all_values[opt[:using]]
144
+ do_populate(elt, values, opt[:using], opt[:as], opt[:populate])
164
145
  end
165
146
  elt
166
147
  end
167
148
  end
168
149
  end
169
150
 
151
+ def do_populate(elt, values, using, as, populate)
152
+ if populate.is_a? Array
153
+ populate.collect do |_key|
154
+ alias_method = find_alias(as, _key)
155
+ parsed_key = JSON.parse(values[elt.with_indifferent_access[using]]).deep_symbolize_keys
156
+ elt[alias_method] = parsed_key[_key]
157
+ end
158
+ else
159
+ alias_method = find_alias(as, populate)
160
+ elt[alias_method] = values[elt.with_indifferent_access[using]]
161
+ end
162
+ end
163
+
164
+
170
165
  def __entity__(entity)
171
166
  entity.to_s.gsub('_id', '')
172
167
  end
@@ -1,3 +1,3 @@
1
1
  module Lookist
2
- VERSION = '0.3.6'
2
+ VERSION = '0.3.7'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: looksist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - RC
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-05 00:00:00.000000000 Z
12
+ date: 2014-11-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler