looksist 0.3.1 → 0.3.2

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: 4435c44a529b1e8c3d6a4aa5d30dc607bee594d6
4
- data.tar.gz: 742014189b7c80b9e461fc7e83a048414160a009
3
+ metadata.gz: 53a388186cedd458e2dab2eecb8b05e9351c81a4
4
+ data.tar.gz: 9709f69d2f4ba76a3f3ef6e24e04d5f74d628c0d
5
5
  SHA512:
6
- metadata.gz: 031b61bef8712170f79e6012f70c91f5942bd8661bbc0abc447bad35b07cb94af3e24dc45433c364d27e5b3ae2f28dea5f55443d7c9edf943ab82b0ac0f94e89
7
- data.tar.gz: 47c2b8d8cbef46989cd8abd36131116656849cf317149901b6a97936ab5e1b12e183b0d7da1c6bbe643b1188ea5fe1bcdeb8d81f2b0064068a883e06cc1b8719
6
+ metadata.gz: 4ccc8051b94102c289c1e5a9463707ef559e7d82ab0e2a35fb11730ac5977dd8bab91c1a09a31caf45fc013d5e28e0804d158b91e15c2e273aff34fc04dab536
7
+ data.tar.gz: 9af59f885b033c6f8da30ad4d0faba4edc3d305cce38ba5cff2758ed4a0954ae72573cba46196a8156ac64992d4867212f5bf80dd5c296d94c70891f20bde724
data/README.md CHANGED
@@ -221,7 +221,7 @@ it 'should inject multiple attribute to an existing deep hash for class methods'
221
221
  }
222
222
  end
223
223
 
224
- class_inject after: :metrics, at: '$.table.database',
224
+ inject after: :metrics, at: '$.table.database',
225
225
  using: :employee_id, populate: :employee_name
226
226
 
227
227
  end
@@ -15,42 +15,42 @@ module Looksist
15
15
  (@rules[after] ||= []) << opts
16
16
 
17
17
  unless @rules[after].length > 1
18
- define_method("#{after}_with_inject") do |*args|
19
- hash = send("#{after}_without_inject".to_sym, *args)
20
- self.class.instance_variable_get(:@rules)[after].each do |opts|
21
- if opts[:at].nil? or opts[:at].is_a? String
22
- hash = self.class.update_using_json_path(hash, opts)
23
- else
24
- self.class.inject_attributes_at(hash[opts[:at]], opts)
25
- end
26
- end
27
- hash
18
+ if self.singleton_methods.include?(after)
19
+ inject_class_methods(after)
20
+ else
21
+ inject_instance_methods(after)
28
22
  end
29
- alias_method_chain after, :inject
30
23
  end
31
24
  end
32
25
 
33
- def class_inject(opts)
34
- raise 'Incorrect usage' unless [:after, :using, :populate].all? { |e| opts.keys.include? e }
35
-
36
- after = opts[:after]
37
- @rules ||= {}
38
- (@rules[after] ||= []) << opts
26
+ def inject_instance_methods(after)
27
+ define_method("#{after}_with_inject") do |*args|
28
+ hash = send("#{after}_without_inject".to_sym, *args)
29
+ self.class.instance_variable_get(:@rules)[after].each do |opts|
30
+ if opts[:at].nil? or opts[:at].is_a? String
31
+ hash = self.class.update_using_json_path(hash, opts)
32
+ else
33
+ self.class.inject_attributes_at(hash[opts[:at]], opts)
34
+ end
35
+ end
36
+ hash
37
+ end
38
+ alias_method_chain after, :inject
39
+ end
39
40
 
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].nil? or 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
41
+ def inject_class_methods(after)
42
+ define_singleton_method("#{after}_with_inject") do |*args|
43
+ hash = send("#{after}_without_inject".to_sym, *args)
44
+ @rules[after].each do |opts|
45
+ if opts[:at].nil? or opts[:at].is_a? String
46
+ hash = update_using_json_path(hash, opts)
47
+ else
48
+ inject_attributes_at(hash[opts[:at]], opts)
49
49
  end
50
- hash
51
50
  end
52
- self.singleton_class.send(:alias_method_chain, after, :inject)
51
+ hash
53
52
  end
53
+ self.singleton_class.send(:alias_method_chain, after, :inject)
54
54
  end
55
55
 
56
56
  def inject_attributes_at(hash_offset, opts)
@@ -71,7 +71,7 @@ module Looksist
71
71
  i
72
72
  end
73
73
  else
74
- inject_attributes_at(hash, opts)
74
+ inject_attributes_at(hash, opts)
75
75
 
76
76
  end.to_hash.deep_symbolize_keys
77
77
  else
@@ -1,3 +1,3 @@
1
1
  module Lookist
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
@@ -464,12 +464,31 @@ describe Looksist::Hashed do
464
464
  ]
465
465
  end
466
466
 
467
- class_inject after: :help_me, at: '$', using: :a, bucket_name: 'ids', populate: :name
467
+ inject after: :help_me, at: '$', using: :a, bucket_name: 'ids', populate: :name
468
468
  end
469
469
 
470
- expect(@mock).to receive(:mget).once.with('ids/1').and_return(['RC'])
471
- expect(SelfHelp.help_me).to eq([{:a => 1, :name => "RC"}])
470
+ expect(@mock).to receive(:mget).once.with('ids/1').and_return(['RajiniKanth'])
471
+ expect(SelfHelp.help_me).to eq([{:a => 1, :name => "RajiniKanth"}])
472
+ end
473
+
474
+ it 'should work for first level hashes in class methods' do
475
+ class FirstLevelClass
476
+ include Looksist
477
+
478
+ class << self
479
+ def my_method
480
+ {
481
+ a: 1
482
+ }
483
+ end
484
+ end
485
+
486
+ inject after: :my_method, using: :a, bucket_name: 'ids', populate: :name
487
+ end
488
+
489
+ expect(@mock).to receive(:get).once.with('ids/1').and_return('RajiniKanth')
490
+ expect(FirstLevelClass.my_method).to eq({:a => 1, :name => "RajiniKanth"})
472
491
  end
473
492
 
474
493
  end
475
- end
494
+ 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.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - RC