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 +4 -4
- data/lib/looksist/core.rb +12 -8
- data/lib/looksist/version.rb +1 -1
- data/spec/looksist/looksist_spec.rb +21 -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: 3505080addc197736b40fd6384223c3f116d94bc
|
4
|
+
data.tar.gz: 9a3303eb8dd99a337dfb74e669ce9cfd970294ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1606ba5e94a4a323a6ce83b4c7804519bb169ba670bba6d7fc29da7488ac972d459815277b904c631b0932d62a54bab8f4bf34d8a5fbc6257daf3d822437446
|
7
|
+
data.tar.gz: f72d42711b96d872d972f397b94c6104724e32cb93d2b4cb7bfdf159522f1d11d8bcbbfa6aff5a65b9bc7dc73f26f53ef96c20d9e59f2d5bf17ac72760ffe7c8
|
data/lib/looksist/core.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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.
|
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
|
data/lib/looksist/version.rb
CHANGED
@@ -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
|