looksist 0.0.5 → 0.0.6
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 +10 -1
- data/lib/looksist/version.rb +1 -1
- data/spec/hashed_spec.rb +87 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47127df0b135086ad1b95ee199d088314c50f83c
|
4
|
+
data.tar.gz: 198c22a471345cc532722857028da3606c6391e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e58ac9cddc8cca41531ba83705e1943599a6873071f484689edc73c384dbf909a4936eebed6965336ff4efa79f27d35f28cfeab3c8ab916e6709d79c9d492bb1
|
7
|
+
data.tar.gz: 34ef32317da4f0fd82c7524b87c90dc56c44cb7dd9bff680830d9cd04ec6a6de9b9b5c5cc7cf7b3dcf9c3fcf9d6755d6292b979cf702af521ee45ccbfeaa924d
|
data/lib/looksist/hashed.rb
CHANGED
@@ -23,7 +23,7 @@ module Looksist
|
|
23
23
|
self.class.instance_variable_get(:@rules)[opts[:after]].each do |opts|
|
24
24
|
if opts[:at].is_a? String
|
25
25
|
hash = JsonPath.for(hash.with_indifferent_access).gsub(opts[:at]) do |i|
|
26
|
-
inject_attributes_at(i, opts)
|
26
|
+
i.is_a?(Array) ? inject_attributes_for(i, opts) : inject_attributes_at(i, opts)
|
27
27
|
end.to_hash.deep_symbolize_keys
|
28
28
|
else
|
29
29
|
inject_attributes_at(hash[opts[:at]], opts)
|
@@ -51,5 +51,14 @@ module Looksist
|
|
51
51
|
hash_offset[opts[:populate]] = values
|
52
52
|
hash_offset
|
53
53
|
end
|
54
|
+
|
55
|
+
def inject_attributes_for(arry_of_hashes, opts)
|
56
|
+
arry_of_hashes.each do |elt|
|
57
|
+
key = elt[opts[:using]]
|
58
|
+
entity_name = entity(opts[:using])
|
59
|
+
value = Hashed.redis_service.send("#{entity_name}_for", [key])
|
60
|
+
elt[opts[:populate]] = value.first
|
61
|
+
end
|
62
|
+
end
|
54
63
|
end
|
55
64
|
end
|
data/lib/looksist/version.rb
CHANGED
data/spec/hashed_spec.rb
CHANGED
@@ -17,7 +17,94 @@ describe Looksist::Hashed do
|
|
17
17
|
|
18
18
|
|
19
19
|
context 'inject ' do
|
20
|
+
|
20
21
|
it 'should be capable to deep lookup and inject' do
|
22
|
+
class Menu
|
23
|
+
include Looksist::Hashed
|
24
|
+
|
25
|
+
def metrics
|
26
|
+
{
|
27
|
+
table: {
|
28
|
+
menu: [
|
29
|
+
{
|
30
|
+
item_id: 1
|
31
|
+
},
|
32
|
+
{
|
33
|
+
item_id: 2
|
34
|
+
}
|
35
|
+
]
|
36
|
+
}
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
inject after: :metrics, at: '$.table.menu', using: :item_id, populate: :item_name
|
41
|
+
end
|
42
|
+
|
43
|
+
expect(@mock).to receive(:get).with('items/1').and_return(OpenStruct.new(value: 'Idly'))
|
44
|
+
expect(@mock).to receive(:get).with('items/2').and_return(OpenStruct.new(value: 'Pongal'))
|
45
|
+
|
46
|
+
expect(Menu.new.metrics).to eq(
|
47
|
+
{
|
48
|
+
table: {
|
49
|
+
menu: [
|
50
|
+
{
|
51
|
+
item_id: 1,
|
52
|
+
item_name: 'Idly'
|
53
|
+
},
|
54
|
+
{
|
55
|
+
item_id: 2,
|
56
|
+
item_name: 'Pongal'
|
57
|
+
}
|
58
|
+
]
|
59
|
+
}
|
60
|
+
}
|
61
|
+
)
|
62
|
+
end
|
63
|
+
|
64
|
+
xit 'should be capable to deep lookup and inject - another example' do
|
65
|
+
class NewMenu
|
66
|
+
include Looksist::Hashed
|
67
|
+
|
68
|
+
def metrics
|
69
|
+
{
|
70
|
+
table: {
|
71
|
+
menu: [
|
72
|
+
{
|
73
|
+
item_id: 4,
|
74
|
+
item_name: 'Idly'
|
75
|
+
},
|
76
|
+
{
|
77
|
+
item_id: 5
|
78
|
+
}
|
79
|
+
]
|
80
|
+
}
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
inject after: :metrics, at: '$.table.menu[?(@.item_id=5)]', using: :item_id, populate: :item_name
|
85
|
+
end
|
86
|
+
|
87
|
+
expect(@mock).to receive(:get).with('items/5').and_return(OpenStruct.new(value: 'Pongal'))
|
88
|
+
|
89
|
+
expect(NewMenu.new.metrics).to eq(
|
90
|
+
{
|
91
|
+
table: {
|
92
|
+
menu: [
|
93
|
+
{
|
94
|
+
item_id: 4,
|
95
|
+
item_name: 'Idly'
|
96
|
+
},
|
97
|
+
{
|
98
|
+
item_id: 5,
|
99
|
+
item_name: 'Pongal'
|
100
|
+
}
|
101
|
+
]
|
102
|
+
}
|
103
|
+
}
|
104
|
+
)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should be capable to deep lookup and inject on columnar hashes' do
|
21
108
|
class DeepHash
|
22
109
|
include Looksist::Hashed
|
23
110
|
|