looksist 0.3.4 → 0.3.5

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: c9e223a56fa500fbab484ad09eb23888d21ba0e4
4
- data.tar.gz: c9e15de4a77e2f64a0458bb4f9a05fd89127c22f
3
+ metadata.gz: 5d8ea751e7e460f9cefd33a35bde5d1ec4e1b3be
4
+ data.tar.gz: 19a868515f44ac3daa6861b22f29a6ea7c67366a
5
5
  SHA512:
6
- metadata.gz: e29215c24a0879bd9c971539d52d4c34d109de530d9f53d6488fd10a333af560b79e32c01defa2c7775b33c73380f35fe24bb225c6eb553ee6b028866a59b27a
7
- data.tar.gz: c6bbf9e15bea1037dc15f4223503992392144e023a98c80db915c20af9d8ad0263c9c260ad94f936b877d4891d5d7254bdd860e1ec04c4cfaf88ceab9ff5db7b
6
+ metadata.gz: d44bdc278b7e5f4d6fec53b2ba1954f10ce50f2580d594125ad0a3477133c7ecc055ede69cecc2da78a45d46b520d44552baa09d565889dd3d2b834f1ae36ab8
7
+ data.tar.gz: 6e0062819715018e8dd6dceeace7e94d7d3511d9d233d8fa1c09402043a625e6f2da46669ca52fb3566870e8c80481a6ab1fc12ac340f566bcacf95f20d04bf6
@@ -12,5 +12,5 @@ class ArrayOfHash
12
12
  ]
13
13
  end
14
14
 
15
- inject after: :menu, at: '$', using: :item_id, populate: :item_name
15
+ inject after: :menu, using: :item_id, populate: :item_name
16
16
  end
@@ -61,11 +61,7 @@ module Looksist
61
61
  if opts[:populate].is_a? Array
62
62
  opts[:populate].each do |elt|
63
63
  value_hash = values.each_with_object([]) do |i, acc|
64
- if i.nil?
65
- acc << nil
66
- else
67
- acc << JSON.parse(i).deep_symbolize_keys[elt]
68
- end
64
+ acc << JSON.parse(i || '{}').deep_symbolize_keys[elt]
69
65
  end
70
66
  alias_method = find_alias(opts[:as], elt)
71
67
  hash_offset[alias_method] = value_hash
@@ -89,7 +85,7 @@ module Looksist
89
85
 
90
86
  end.to_hash.deep_symbolize_keys
91
87
  else
92
- inject_attributes_for(hash, opts)
88
+ inject_attributes_for_array(hash, opts)
93
89
  end
94
90
  end
95
91
 
@@ -100,6 +96,19 @@ module Looksist
100
96
  opts[:populate].is_a?(Array) ? composite_attribute_lookup(array_of_hashes, opts, values) : single_attribute_lookup(array_of_hashes, opts, values)
101
97
  end
102
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
+
103
112
  def single_attribute_lookup(array_of_hashes, opts, values)
104
113
  array_of_hashes.each do |elt|
105
114
  alias_method = find_alias(opts[:as], opts[:populate])
@@ -107,6 +116,25 @@ module Looksist
107
116
  end
108
117
  end
109
118
 
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
135
+ end
136
+ end
137
+
110
138
  def composite_attribute_lookup(array_of_hashes, opts, values)
111
139
  array_of_hashes.each do |elt|
112
140
  opts[:populate].each do |_key|
@@ -1,3 +1,3 @@
1
1
  module Lookist
2
- VERSION = '0.3.4'
2
+ VERSION = '0.3.5'
3
3
  end
@@ -372,7 +372,7 @@ describe Looksist::Hashed do
372
372
  }})
373
373
  end
374
374
 
375
- it 'should work for first level of substitution' do
375
+ it 'should work for first level of substitution' do
376
376
  class FirstLevelHash
377
377
  include Looksist
378
378
 
@@ -386,9 +386,9 @@ describe Looksist::Hashed do
386
386
  expect(@mock).to receive(:get).with('employees/10').and_return('emp 1')
387
387
 
388
388
  expect(FirstLevelHash.new.metrics).to eq({
389
- employee_id: 10,
390
- employee_name: 'emp 1'
391
- })
389
+ employee_id: 10,
390
+ employee_name: 'emp 1'
391
+ })
392
392
  end
393
393
 
394
394
  it 'should work for array of hashes' do
@@ -406,7 +406,7 @@ describe Looksist::Hashed do
406
406
  ]
407
407
  end
408
408
 
409
- inject after: :metrics, at: '$', using: :item_id, populate: :item_name, as: {item_name: 'dish_name'}
409
+ inject after: :metrics, using: :item_id, populate: :item_name, as: {item_name: 'dish_name'}
410
410
  end
411
411
 
412
412
  expect(@mock).to receive(:mget).once.with(*%w(items/1 items/2)).and_return(%w(Idly Pongal))
@@ -447,8 +447,8 @@ describe Looksist::Hashed do
447
447
  expect(@mock).to receive(:mget).once.with(*%w(heros/1 heros/2)).and_return(jsons)
448
448
 
449
449
  expect(HashWithMultipleAttributes.new.metrics).to eq(
450
- [{:hero_id => 1, :hero_name => "Rajini", :hero_mnemonic => "SuperStart"},
451
- {:hero_id => 2, :hero_name => "Kamal", :hero_mnemonic => "Ulaganayagan"}]
450
+ [{:hero_id => 1, :hero_name => 'Rajini', :hero_mnemonic => 'SuperStart'},
451
+ {:hero_id => 2, :hero_name => 'Kamal', :hero_mnemonic => 'Ulaganayagan'}]
452
452
  )
453
453
  end
454
454
 
@@ -464,11 +464,11 @@ describe Looksist::Hashed do
464
464
  ]
465
465
  end
466
466
 
467
- inject after: :help_me, at: '$', using: :a, bucket_name: 'ids', populate: :name
467
+ inject after: :help_me, using: :a, bucket_name: 'ids', populate: :name
468
468
  end
469
469
 
470
470
  expect(@mock).to receive(:mget).once.with('ids/1').and_return(['RajiniKanth'])
471
- expect(SelfHelp.help_me).to eq([{:a => 1, :name => "RajiniKanth"}])
471
+ expect(SelfHelp.help_me).to eq([{:a => 1, :name => 'RajiniKanth'}])
472
472
  end
473
473
 
474
474
  it 'should work for first level hashes in class methods' do
@@ -477,9 +477,9 @@ describe Looksist::Hashed do
477
477
 
478
478
  class << self
479
479
  def my_method
480
- {
481
- a: 1
482
- }
480
+ {
481
+ a: 1
482
+ }
483
483
  end
484
484
  end
485
485
 
@@ -487,7 +487,7 @@ describe Looksist::Hashed do
487
487
  end
488
488
 
489
489
  expect(@mock).to receive(:get).once.with('ids/1').and_return('RajiniKanth')
490
- expect(FirstLevelClass.my_method).to eq({:a => 1, :name => "RajiniKanth"})
490
+ expect(FirstLevelClass.my_method).to eq({:a => 1, :name => 'RajiniKanth'})
491
491
  end
492
492
 
493
493
  it 'should be capable to deep lookup and inject multiple attributes' do
@@ -497,14 +497,14 @@ describe Looksist::Hashed do
497
497
  def self.metrics
498
498
  {
499
499
  table: {
500
- menu:{
501
- item_id: [1,2]
500
+ menu: {
501
+ item_id: [1, 2]
502
+ }
502
503
  }
503
- }
504
504
  }
505
505
  end
506
506
 
507
- inject after: :metrics, at: '$.table.menu', using: :item_id, populate: [:name, :mnemonic], as: {name:'item_name', mnemonic:'item_mnemonic'}
507
+ inject after: :metrics, at: '$.table.menu', using: :item_id, populate: [:name, :mnemonic], as: {name: 'item_name', mnemonic: 'item_mnemonic'}
508
508
  end
509
509
 
510
510
  js1 = {name: 'Rice Cake', mnemonic: 'Idly'}.to_json
@@ -513,7 +513,7 @@ describe Looksist::Hashed do
513
513
 
514
514
  expect(@mock).to receive(:mget).once.with(*%w(items/1 items/2)).and_return(jsons)
515
515
 
516
- expect(DeepLookUpMultiple.metrics).to eq({:table => {:menu=>{:item_id=>[1, 2], :item_name=>["Rice Cake", "Rice pudding"], :item_mnemonic=>["Idly", "Pongal"]}}})
516
+ expect(DeepLookUpMultiple.metrics).to eq({:table => {:menu => {:item_id => [1, 2], :item_name => ['Rice Cake', 'Rice pudding'], :item_mnemonic => ['Idly', 'Pongal']}}})
517
517
  end
518
518
 
519
519
  it 'should be capable to deep lookup and inject multiple attributes ignoring nil values' do
@@ -523,14 +523,14 @@ describe Looksist::Hashed do
523
523
  def self.metrics
524
524
  {
525
525
  table: {
526
- menu:{
527
- item_id: [1,2]
526
+ menu: {
527
+ item_id: [1, 2]
528
+ }
528
529
  }
529
- }
530
530
  }
531
531
  end
532
532
 
533
- inject after: :metrics, at: '$.table.menu', using: :item_id, populate: [:name, :mnemonic], as: {name:'item_name', mnemonic:'item_mnemonic'}
533
+ inject after: :metrics, at: '$.table.menu', using: :item_id, populate: [:name, :mnemonic], as: {name: 'item_name', mnemonic: 'item_mnemonic'}
534
534
  end
535
535
 
536
536
  js1 = {name: 'Rice Cake', mnemonic: 'Idly'}.to_json
@@ -538,7 +538,7 @@ describe Looksist::Hashed do
538
538
 
539
539
  expect(@mock).to receive(:mget).once.with(*%w(items/1 items/2)).and_return(jsons)
540
540
 
541
- expect(DeepLookUpMultipleIngnorNil.metrics).to eq({:table => {:menu=>{:item_id=>[1, 2], :item_name=>["Rice Cake", nil], :item_mnemonic=>["Idly", nil]}}})
541
+ expect(DeepLookUpMultipleIngnorNil.metrics).to eq({:table => {:menu => {:item_id => [1, 2], :item_name => ['Rice Cake', nil], :item_mnemonic => ['Idly', nil]}}})
542
542
  end
543
543
 
544
544
  it 'should be capable to deep lookup and inject multiple attributes in same order' do
@@ -548,25 +548,69 @@ describe Looksist::Hashed do
548
548
  def self.metrics
549
549
  {
550
550
  table: {
551
- menu:{
552
- item_id: [1,2,2,1,1,2,3,3,2,1]
551
+ menu: {
552
+ item_id: [1, 2, 2, 1, 1, 2, 3, 3, 2, 1]
553
+ }
554
+ }
555
+ }
556
+ end
557
+
558
+ inject after: :metrics, at: '$.table.menu', using: :item_id, populate: [:name, :mnemonic], as: {name: 'item_name', mnemonic: 'item_mnemonic'}
559
+ end
560
+
561
+ class ColumnarWithEmpty
562
+ include Looksist
563
+
564
+ def self.metrics
565
+ {
566
+ table: {
567
+ menu: {
568
+ item_id: []
569
+ }
553
570
  }
554
- }
555
571
  }
556
572
  end
557
573
 
558
- inject after: :metrics, at: '$.table.menu', using: :item_id, populate: [:name, :mnemonic], as: {name:'item_name', mnemonic:'item_mnemonic'}
574
+ inject after: :metrics, at: '$.table.menu', using: :item_id, populate: [:name, :mnemonic], as: {name: 'item_name', mnemonic: 'item_mnemonic'}
559
575
  end
560
576
 
561
577
  js1 = {name: 'Rice Cake', mnemonic: 'Idly'}.to_json
562
578
  js2 = {name: 'Pan Cake', mnemonic: 'Dosa'}.to_json
563
- jsons = [js1, js2, nil]
579
+ jsons = [js1, js2, nil]
564
580
 
565
581
  expect(@mock).to receive(:mget).once.with(*%w(items/1 items/2 items/3)).and_return(jsons)
566
582
 
567
- expect(ColumnarWithNil.metrics).to eq({:table => {:menu=>{:item_id=>[1,2,2,1,1,2,3,3,2,1], :item_name=>["Rice Cake", "Pan Cake", "Pan Cake", "Rice Cake", "Rice Cake", "Pan Cake", nil, nil, "Pan Cake", "Rice Cake"],
568
- :item_mnemonic=>["Idly", "Dosa","Dosa","Idly","Idly","Dosa", nil,nil, "Dosa", "Idly" ]}}})
583
+ expect(ColumnarWithNil.metrics).to eq({:table => {:menu => {
584
+ :item_id => [1, 2, 2, 1, 1, 2, 3, 3, 2, 1],
585
+ :item_name => ['Rice Cake', 'Pan Cake', 'Pan Cake', 'Rice Cake', 'Rice Cake', 'Pan Cake', nil, nil, 'Pan Cake', 'Rice Cake'],
586
+ :item_mnemonic => ['Idly', 'Dosa', 'Dosa', 'Idly', 'Idly', 'Dosa', nil, nil, 'Dosa', 'Idly']}}})
587
+
588
+ expect(ColumnarWithEmpty.metrics).to eq({
589
+ table: {
590
+ menu: {
591
+ item_id: [],
592
+ item_name: [],
593
+ item_mnemonic: []
594
+ }
595
+ }
596
+ })
569
597
  end
570
598
 
599
+ it'should work for nested injection for array of hash' do
600
+ class DeepLookUpAtArrayOfHash
601
+ include Looksist
602
+ def self.articles
603
+ [{
604
+ articles:[{article_id: 1}, {article_id: 2}]
605
+ }]
606
+ end
607
+ inject after: :articles, at:'$..articles', using: :article_id, populate: :name
608
+ end
609
+
610
+ expect(@mock).to receive(:mget).once.with(*%w(articles/1 articles/2)).and_return(['a','b'])
611
+
612
+ expect(DeepLookUpAtArrayOfHash.articles).to eq([{articles:[{article_id: 1, name:'a'}, {article_id:2, name:'b'}]}])
613
+
614
+ end
571
615
  end
572
616
  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.4
4
+ version: 0.3.5
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-31 00:00:00.000000000 Z
12
+ date: 2014-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler