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 +4 -4
- data/lib/looksist/hashed.rb +78 -83
- data/lib/looksist/version.rb +1 -1
- 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: 35a51afb4989fbb14f003fa155e50e14afb90439
|
4
|
+
data.tar.gz: 4295b2ff68f81062ab5d2911302de3998a0d22f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 179e3288026145a68f0548aed4e284d104b92354db7583cba9f64b463d627094efacb2cf8ae73d6f865974a4c78dbfccf68aae00ff510203fcc445a7ee188308
|
7
|
+
data.tar.gz: a664389618be8eb5f89032d69ba1ddfd422b2c3868b13572f3b6b1be8388e8315709927adb1dc9c072e8c47e69171517ee1d8337f422e77d5e7fcda751c0f8d7
|
data/lib/looksist/hashed.rb
CHANGED
@@ -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].
|
30
|
-
|
31
|
-
|
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[
|
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].
|
45
|
-
|
46
|
-
|
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[
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
67
|
-
|
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
|
79
|
-
JsonPath.for(hash.with_indifferent_access).gsub!(
|
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
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
120
|
-
|
121
|
-
|
122
|
-
if
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
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
|
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
|
141
|
-
JsonPath.for(elt.with_indifferent_access).gsub!(
|
124
|
+
if at.present?
|
125
|
+
JsonPath.for(elt.with_indifferent_access).gsub!(at) do |node|
|
142
126
|
if node.is_a? Array
|
143
|
-
node.
|
144
|
-
opts
|
145
|
-
|
146
|
-
|
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
|
-
|
152
|
-
|
153
|
-
|
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
|
-
|
161
|
-
|
162
|
-
|
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
|
data/lib/looksist/version.rb
CHANGED
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.
|
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-
|
12
|
+
date: 2014-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|