looksist 0.2.9 → 0.2.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -0
- data/lib/looksist/hashed.rb +23 -2
- data/lib/looksist/version.rb +1 -1
- data/spec/looksist/hashed_spec.rb +40 -11
- 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: 357a6b9dc9b97cc730994ffefd01db24f4349afc
|
4
|
+
data.tar.gz: a13b5765b68bc304bb0ee08725d20ce3cc8411a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84d35f48ca1af7fcba1ef5656f23cdf1cff8a389c46c95bcd709026b74a60307ebd00bf5f1d62f7d52c188f62758c72e25480b44e02a89ee6a460862d6846740
|
7
|
+
data.tar.gz: 68709cd5211aa716e5a22ac8f91cd35e45a3e2147107e4511f606d06725acd9d604b584d2821660e92af104cc4f5f9f19b3d07713184b4fba1153c7dd525c352
|
data/README.md
CHANGED
@@ -83,6 +83,35 @@ lookup [:name, :age], using: :id, as: {name: 'nome'}
|
|
83
83
|
### With Plain Hashes
|
84
84
|
|
85
85
|
|
86
|
+
#### Array Of Hashes
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
it 'should inject single attribute into array of hashes' do
|
90
|
+
class HashService
|
91
|
+
include Looksist
|
92
|
+
|
93
|
+
def metrics
|
94
|
+
[
|
95
|
+
{
|
96
|
+
employee_id: 5
|
97
|
+
},
|
98
|
+
{
|
99
|
+
employer_id: 3
|
100
|
+
}
|
101
|
+
[
|
102
|
+
end
|
103
|
+
|
104
|
+
inject after: :metrics, at: '$',
|
105
|
+
using: :employee_id, populate: :employee_name
|
106
|
+
end
|
107
|
+
# Removed mock expectations, look at the tests for actuals
|
108
|
+
expect(HashService.new.metrics).to eq([{employeed_id: 5, employee_name: 'Emp 5'},{employeed_id: 3, employee_name: 'Emp 3'}])
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
```
|
113
|
+
|
114
|
+
|
86
115
|
#### Columnar Hashes
|
87
116
|
|
88
117
|
* First Level look ups
|
data/lib/looksist/hashed.rb
CHANGED
@@ -45,7 +45,8 @@ module Looksist
|
|
45
45
|
keys = hash_offset[opts[:using]]
|
46
46
|
entity_name = __entity__(opts[:bucket_name] || opts[:using])
|
47
47
|
values = Looksist.redis_service.send("#{entity_name}_for", keys)
|
48
|
-
|
48
|
+
alias_method = find_alias(opts, opts[:populate])
|
49
|
+
hash_offset[alias_method] = values
|
49
50
|
hash_offset
|
50
51
|
end
|
51
52
|
|
@@ -65,9 +66,29 @@ module Looksist
|
|
65
66
|
entity_name = __entity__(opts[:bucket_name] || opts[:using])
|
66
67
|
keys = (arry_of_hashes.collect { |i| i[opts[:using]] }).compact.uniq
|
67
68
|
values = Hash[keys.zip(Looksist.redis_service.send("#{entity_name}_for", keys))]
|
69
|
+
opts[:populate].is_a?(Array) ? composite_attribute_lookup(arry_of_hashes, opts, values) : single_attribute_lookup(arry_of_hashes, opts, values)
|
70
|
+
end
|
71
|
+
|
72
|
+
def single_attribute_lookup(arry_of_hashes, opts, values)
|
68
73
|
arry_of_hashes.each do |elt|
|
69
|
-
|
74
|
+
alias_method = find_alias(opts, opts[:populate])
|
75
|
+
elt[alias_method] = values[elt[opts[:using]]]
|
70
76
|
end
|
71
77
|
end
|
78
|
+
|
79
|
+
def composite_attribute_lookup(arry_of_hashes, opts, values)
|
80
|
+
arry_of_hashes.each do |elt|
|
81
|
+
opts[:populate].each do |_key|
|
82
|
+
parsed_key = JSON.parse(values[elt[opts[:using]]]).deep_symbolize_keys
|
83
|
+
alias_method = find_alias(opts, _key)
|
84
|
+
elt[alias_method] = parsed_key[_key]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def find_alias(opts,what)
|
90
|
+
as_map = opts[:as]
|
91
|
+
(as_map and as_map.has_key?(what)) ? as_map[what].to_sym : what
|
92
|
+
end
|
72
93
|
end
|
73
94
|
end
|
data/lib/looksist/version.rb
CHANGED
@@ -380,27 +380,56 @@ describe Looksist::Hashed do
|
|
380
380
|
[
|
381
381
|
{
|
382
382
|
item_id: 1
|
383
|
-
|
383
|
+
},
|
384
384
|
{
|
385
385
|
item_id: 2
|
386
|
-
|
386
|
+
}
|
387
387
|
]
|
388
388
|
end
|
389
389
|
|
390
|
-
inject after: :metrics, at: '$', using: :item_id, populate: :item_name
|
390
|
+
inject after: :metrics, at: '$', using: :item_id, populate: :item_name, as: {item_name: 'dish_name'}
|
391
391
|
end
|
392
392
|
|
393
393
|
expect(@mock).to receive(:mget).once.with(*%w(items/1 items/2)).and_return(%w(Idly Pongal))
|
394
394
|
|
395
395
|
expect(ArrayOfHashes.new.metrics).to eq(
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
396
|
+
[{
|
397
|
+
item_id: 1,
|
398
|
+
dish_name: 'Idly'
|
399
|
+
},
|
400
|
+
{
|
401
|
+
item_id: 2,
|
402
|
+
dish_name: 'Pongal'
|
403
|
+
}]
|
404
|
+
)
|
405
|
+
|
406
|
+
end
|
407
|
+
|
408
|
+
it 'should work for multiple attributes' do
|
409
|
+
class HashWithMultipleAttributes
|
410
|
+
include Looksist
|
411
|
+
|
412
|
+
def metrics
|
413
|
+
[
|
414
|
+
{
|
415
|
+
hero_id: 1
|
416
|
+
},
|
417
|
+
{
|
418
|
+
hero_id: 2
|
419
|
+
}
|
420
|
+
]
|
421
|
+
end
|
422
|
+
|
423
|
+
inject after: :metrics, at: '$', using: :hero_id, populate: [:name,:mnemonic], as: {name: 'hero_name', mnemonic: 'hero_mnemonic'}
|
424
|
+
end
|
425
|
+
js1 = {name: 'Rajini', mnemonic: 'SuperStart'}.to_json
|
426
|
+
js2 = {name: 'Kamal', mnemonic: 'Ulaganayagan'}.to_json
|
427
|
+
jsons = [js1,js2]
|
428
|
+
expect(@mock).to receive(:mget).once.with(*%w(heros/1 heros/2)).and_return(jsons)
|
429
|
+
|
430
|
+
expect(HashWithMultipleAttributes.new.metrics).to eq(
|
431
|
+
[{:hero_id=>1, :hero_name=>"Rajini", :hero_mnemonic=>"SuperStart"},
|
432
|
+
{:hero_id=>2, :hero_name=>"Kamal", :hero_mnemonic=>"Ulaganayagan"}]
|
404
433
|
)
|
405
434
|
end
|
406
435
|
end
|