looksist 0.2.10 → 0.3.0
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 +68 -47
- data/lib/looksist/version.rb +1 -1
- data/spec/looksist/hashed_spec.rb +29 -9
- 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: 316b9f16c6f5919a7514159b641e9692f8df5d45
|
4
|
+
data.tar.gz: 48bebfb3872430070a3bf65ec85c5ec1b79325d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0820b531e51c3c5738e784774c3f338c2defbe13da4f02d62727ee4d86d29c9a2c833f41cfdc472807176480d76069d00b29744344b686148869da4e3a24445
|
7
|
+
data.tar.gz: 45cd16626706c6e4725b9fba750979e5885adebbcac455a2ec3065497b6b38262deda4d98b762c06ba1a3e2c246087042215bb7ecaaf721ab3981503d7b4074f
|
data/lib/looksist/hashed.rb
CHANGED
@@ -19,9 +19,9 @@ module Looksist
|
|
19
19
|
hash = send("#{after}_without_inject".to_sym, *args)
|
20
20
|
self.class.instance_variable_get(:@rules)[after].each do |opts|
|
21
21
|
if opts[:at].is_a? String
|
22
|
-
hash = update_using_json_path(hash, opts)
|
22
|
+
hash = self.class.update_using_json_path(hash, opts)
|
23
23
|
else
|
24
|
-
inject_attributes_at(hash[opts[:at]], opts)
|
24
|
+
self.class.inject_attributes_at(hash[opts[:at]], opts)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
hash
|
@@ -30,65 +30,86 @@ module Looksist
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
def class_inject(opts)
|
34
|
+
raise 'Incorrect usage' unless [:after, :using, :populate].all? { |e| opts.keys.include? e }
|
33
35
|
|
34
|
-
|
36
|
+
after = opts[:after]
|
37
|
+
@rules ||= {}
|
38
|
+
(@rules[after] ||= []) << opts
|
35
39
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
+
unless @rules[after].length > 1
|
41
|
+
define_singleton_method("#{after}_with_inject") do |*args|
|
42
|
+
hash = send("#{after}_without_inject".to_sym, *args)
|
43
|
+
@rules[after].each do |opts|
|
44
|
+
if opts[:at].is_a? String
|
45
|
+
hash = update_using_json_path(hash, opts)
|
46
|
+
else
|
47
|
+
inject_attributes_at(hash[opts[:at]], opts)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
hash
|
51
|
+
end
|
52
|
+
self.singleton_class.send(:alias_method_chain, after, :inject)
|
53
|
+
end
|
54
|
+
end
|
40
55
|
|
41
|
-
|
56
|
+
def inject_attributes_at(hash_offset, opts)
|
57
|
+
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
|
+
alias_method = find_alias(opts, opts[:populate])
|
62
|
+
hash_offset[alias_method] = values
|
63
|
+
hash_offset
|
64
|
+
end
|
42
65
|
|
43
|
-
|
44
|
-
|
45
|
-
keys = hash_offset[opts[:using]]
|
46
|
-
entity_name = __entity__(opts[:bucket_name] || opts[:using])
|
47
|
-
values = Looksist.redis_service.send("#{entity_name}_for", keys)
|
48
|
-
alias_method = find_alias(opts, opts[:populate])
|
49
|
-
hash_offset[alias_method] = values
|
50
|
-
hash_offset
|
51
|
-
end
|
66
|
+
def update_using_json_path(hash, opts)
|
67
|
+
if hash.is_a?(Hash)
|
52
68
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
inject_attributes_for(hash, opts)
|
69
|
+
JsonPath.for(hash.with_indifferent_access).gsub!(opts[:at]) do |i|
|
70
|
+
i.is_a?(Array) ? inject_attributes_for(i, opts) : inject_attributes_at(i, opts) unless (i.nil? or i.empty?)
|
71
|
+
i
|
72
|
+
end.to_hash.deep_symbolize_keys
|
73
|
+
else
|
74
|
+
inject_attributes_for(hash, opts)
|
75
|
+
end
|
61
76
|
end
|
62
|
-
end
|
63
77
|
|
78
|
+
def inject_attributes_for(array_of_hashes, opts)
|
79
|
+
entity_name = __entity__(opts[:bucket_name] || opts[:using])
|
80
|
+
keys = (array_of_hashes.collect { |i| i[opts[:using]] }).compact.uniq
|
81
|
+
values = Hash[keys.zip(Looksist.redis_service.send("#{entity_name}_for", keys))]
|
82
|
+
opts[:populate].is_a?(Array) ? composite_attribute_lookup(array_of_hashes, opts, values) : single_attribute_lookup(array_of_hashes, opts, values)
|
83
|
+
end
|
64
84
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
def single_attribute_lookup(arry_of_hashes, opts, values)
|
73
|
-
arry_of_hashes.each do |elt|
|
74
|
-
alias_method = find_alias(opts, opts[:populate])
|
75
|
-
elt[alias_method] = values[elt[opts[:using]]]
|
85
|
+
def single_attribute_lookup(array_of_hashes, opts, values)
|
86
|
+
array_of_hashes.each do |elt|
|
87
|
+
alias_method = find_alias(opts[:as], opts[:populate])
|
88
|
+
elt[alias_method] = values[elt[opts[:using]]]
|
89
|
+
end
|
76
90
|
end
|
77
|
-
end
|
78
91
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
92
|
+
def composite_attribute_lookup(array_of_hashes, opts, values)
|
93
|
+
array_of_hashes.each do |elt|
|
94
|
+
opts[:populate].each do |_key|
|
95
|
+
parsed_key = JSON.parse(values[elt[opts[:using]]]).deep_symbolize_keys
|
96
|
+
alias_method = find_alias(opts[:as], _key)
|
97
|
+
elt[alias_method] = parsed_key[_key]
|
98
|
+
end
|
85
99
|
end
|
86
100
|
end
|
101
|
+
|
102
|
+
def __entity__(entity)
|
103
|
+
entity.to_s.gsub('_id', '')
|
104
|
+
end
|
105
|
+
|
87
106
|
end
|
88
107
|
|
89
|
-
|
90
|
-
|
91
|
-
|
108
|
+
included do |base|
|
109
|
+
base.class_attribute :rules
|
110
|
+
base.rules = {}
|
92
111
|
end
|
112
|
+
|
113
|
+
|
93
114
|
end
|
94
115
|
end
|
data/lib/looksist/version.rb
CHANGED
@@ -403,9 +403,9 @@ describe Looksist::Hashed do
|
|
403
403
|
}]
|
404
404
|
)
|
405
405
|
|
406
|
-
|
406
|
+
end
|
407
407
|
|
408
|
-
|
408
|
+
it 'should work for multiple attributes' do
|
409
409
|
class HashWithMultipleAttributes
|
410
410
|
include Looksist
|
411
411
|
|
@@ -413,24 +413,44 @@ describe Looksist::Hashed do
|
|
413
413
|
[
|
414
414
|
{
|
415
415
|
hero_id: 1
|
416
|
-
|
416
|
+
},
|
417
417
|
{
|
418
418
|
hero_id: 2
|
419
|
-
|
419
|
+
}
|
420
420
|
]
|
421
421
|
end
|
422
422
|
|
423
|
-
inject after: :metrics, at: '$', using: :hero_id, populate: [:name
|
423
|
+
inject after: :metrics, at: '$', using: :hero_id, populate: [:name, :mnemonic], as: {name: 'hero_name', mnemonic: 'hero_mnemonic'}
|
424
424
|
end
|
425
425
|
js1 = {name: 'Rajini', mnemonic: 'SuperStart'}.to_json
|
426
426
|
js2 = {name: 'Kamal', mnemonic: 'Ulaganayagan'}.to_json
|
427
|
-
jsons = [js1,js2]
|
427
|
+
jsons = [js1, js2]
|
428
428
|
expect(@mock).to receive(:mget).once.with(*%w(heros/1 heros/2)).and_return(jsons)
|
429
429
|
|
430
430
|
expect(HashWithMultipleAttributes.new.metrics).to eq(
|
431
|
-
[{:hero_id=>1, :hero_name=>"Rajini", :hero_mnemonic=>"SuperStart"},
|
432
|
-
|
433
|
-
|
431
|
+
[{:hero_id => 1, :hero_name => "Rajini", :hero_mnemonic => "SuperStart"},
|
432
|
+
{:hero_id => 2, :hero_name => "Kamal", :hero_mnemonic => "Ulaganayagan"}]
|
433
|
+
)
|
434
|
+
end
|
435
|
+
|
436
|
+
it 'should work for class methods' do
|
437
|
+
class SelfHelp
|
438
|
+
include Looksist
|
439
|
+
|
440
|
+
def self.help_me
|
441
|
+
[
|
442
|
+
{
|
443
|
+
a: 1
|
444
|
+
}
|
445
|
+
]
|
446
|
+
end
|
447
|
+
|
448
|
+
class_inject after: :help_me, at: '$', using: :a, bucket_name: 'ids', populate: :name
|
449
|
+
end
|
450
|
+
|
451
|
+
expect(@mock).to receive(:mget).once.with('ids/1').and_return(['RC'])
|
452
|
+
expect(SelfHelp.help_me).to eq([{:a => 1, :name => "RC"}])
|
434
453
|
end
|
454
|
+
|
435
455
|
end
|
436
456
|
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.
|
4
|
+
version: 0.3.0
|
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-10-
|
12
|
+
date: 2014-10-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|