looksist 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b167f1cd856e78371a8a08eb7a16c9b2b05146fa
4
- data.tar.gz: b77389994a04d4527386ed0a9d30594a6e721599
3
+ metadata.gz: 3505080addc197736b40fd6384223c3f116d94bc
4
+ data.tar.gz: 9a3303eb8dd99a337dfb74e669ce9cfd970294ce
5
5
  SHA512:
6
- metadata.gz: 388347f3c8a1d053f9c0a82a19d5561dcb604088198637792e264719a86aed7aa6839245827fcf8d4e9ffee2f91f216cb43919c08da1a60837f7afe3b9b07a40
7
- data.tar.gz: 04893bbe31f866211bde64c6b84d1f394285aeccdec1ee6b39cdfa051a8b2b296def0aceed9dffaae4e6d671f50e2baa9bde9e7a57b1958f1691bcf113a6affc
6
+ metadata.gz: e1606ba5e94a4a323a6ce83b4c7804519bb169ba670bba6d7fc29da7488ac972d459815277b904c631b0932d62a54bab8f4bf34d8a5fbc6257daf3d822437446
7
+ data.tar.gz: f72d42711b96d872d972f397b94c6104724e32cb93d2b4cb7bfdf159522f1d11d8bcbbfa6aff5a65b9bc7dc73f26f53ef96c20d9e59f2d5bf17ac72760ffe7c8
@@ -4,7 +4,11 @@ module Looksist
4
4
  include Looksist::Common
5
5
 
6
6
  module ClassMethods
7
+
8
+ attr_accessor :lookup_attributes
9
+
7
10
  def lookup(what, opts)
11
+ @lookup_attributes ||= {}
8
12
  unless opts.keys.all? { |k| [:using, :bucket_name, :as].include? k }
9
13
  raise 'Incorrect usage: Invalid parameter specified'
10
14
  end
@@ -13,7 +17,7 @@ module Looksist
13
17
  setup_composite_lookup(bucket_name, using, what, as)
14
18
  else
15
19
  alias_what = find_alias(as, what)
16
- self.lookup_attributes << alias_what
20
+ @lookup_attributes[alias_what] = opts[:using]
17
21
  define_method(alias_what) do
18
22
  Looksist.redis_service.send("#{__entity__(bucket_name)}_for", self.send(using).try(:to_s))
19
23
  end
@@ -27,7 +31,7 @@ module Looksist
27
31
  define_method(alias_method_name) do
28
32
  JSON.parse(Looksist.redis_service.send("#{__entity__(bucket)}_for", self.send(using).try(:to_s)) || '{}')[method_name.to_s]
29
33
  end
30
- self.lookup_attributes << alias_method_name
34
+ @lookup_attributes[alias_method_name] = using
31
35
  end
32
36
  end
33
37
 
@@ -41,18 +45,18 @@ module Looksist
41
45
  Looksist.driver.json_opts(self, opts)
42
46
  end
43
47
 
44
- included do |base|
45
- base.class_attribute :lookup_attributes
46
- base.lookup_attributes = []
47
- end
48
-
49
48
  end
50
49
 
51
50
  module Serializers
52
51
  class Her
53
52
  class << self
54
53
  def json_opts(obj, _)
55
- obj.attributes.merge(obj.class.lookup_attributes.each_with_object({}) { |a, acc| acc[a] = obj.send(a) })
54
+ lookup_attributes = obj.class.lookup_attributes || {}
55
+ other_attributes = lookup_attributes.keys.each_with_object({}) do |a, acc|
56
+ using = lookup_attributes[a]
57
+ acc[a] = obj.send(a) if obj.respond_to?(using)
58
+ end
59
+ obj.attributes.merge(other_attributes)
56
60
  end
57
61
  end
58
62
  end
@@ -1,3 +1,3 @@
1
1
  module Lookist
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -51,6 +51,27 @@ describe Looksist do
51
51
  end
52
52
  end
53
53
 
54
+ context 'Tolerant lookup' do
55
+ it 'should not do a lookup when the key attribute is not defined' do
56
+ module TolerantLookUp
57
+ class Employee
58
+ include Her::Model
59
+ use_api TEST_API
60
+ include Looksist
61
+
62
+ lookup :name, using: :employee_id
63
+
64
+ def as_json(opts)
65
+ super(opts)
66
+ end
67
+ end
68
+ end
69
+ expect(@mock).to receive(:get).never.with('employees_/1')
70
+ e = TolerantLookUp::Employee.new
71
+ expect(e.to_json).to eq('{}')
72
+ end
73
+ end
74
+
54
75
  context 'Alias support for lookup' do
55
76
  it 'should fetch attributes and use the alias specified in the api' do
56
77
  module AliasLookup
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.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - RC