looksist 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -6
- data/lib/looksist/core.rb +7 -2
- data/lib/looksist/redis_service.rb +4 -6
- data/lib/looksist/version.rb +1 -1
- data/spec/looksist/hashed_spec.rb +0 -1
- data/spec/looksist/looksist_spec.rb +47 -1
- 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: ac523f9688ba86477a776e9350c37978d8bbd46f
|
4
|
+
data.tar.gz: 4c0f7bdd47f0e43ac9ab7a87fcc8c28b3bb759b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7112c481f716d6d9180c4174cc43e73789aafff7a20173a30f7c4609fd0895ca0890c90dc9c4f7a0a198e068384c35bc3f6675871b76aedd48dae65db7b0cf2
|
7
|
+
data.tar.gz: 3f255b69863bf7bfbac3885af83c32631104861778dea24a27f9b4266de055a1e55a4faa39921f81f090368cff8256d7a6406e4448516df1cd997eb692e0ffe7
|
data/README.md
CHANGED
@@ -31,7 +31,7 @@ Or install it yourself as:
|
|
31
31
|
|
32
32
|
``` ruby
|
33
33
|
Looksist.configure do |looksist|
|
34
|
-
looksist.lookup_store = Redis.new(:url =>
|
34
|
+
looksist.lookup_store = Redis.new(:url => ENV['REDIS_URL'], :driver => :hiredis)
|
35
35
|
looksist.driver = Looksist::Serializers::Her
|
36
36
|
end
|
37
37
|
```
|
@@ -45,7 +45,7 @@ it 'should generate declarative attributes on the model with simple lookup value
|
|
45
45
|
class Employee
|
46
46
|
include Looksist
|
47
47
|
attr_accessor :id
|
48
|
-
lookup :name, using
|
48
|
+
lookup :name, using: :id
|
49
49
|
|
50
50
|
def initialize(id)
|
51
51
|
@id = id
|
@@ -58,18 +58,22 @@ it 'should generate declarative attributes on the model with simple lookup value
|
|
58
58
|
expect(e.name).to eq('Employee Name')
|
59
59
|
end
|
60
60
|
```
|
61
|
-
lookup
|
61
|
+
lookup can take the following forms:
|
62
62
|
|
63
63
|
``` ruby
|
64
64
|
# will lookup "employees/#{employee_id}" from the store
|
65
|
-
lookup :name, using
|
65
|
+
lookup :name, using: :employee_id
|
66
66
|
|
67
67
|
# will lookup "stars/#{employee_id}" from the store
|
68
|
-
lookup :name, using
|
68
|
+
lookup :name, using: :employee_id, bucket_name: "stars"
|
69
69
|
|
70
70
|
# will lookup "stars/#{employee_id}" from the store
|
71
71
|
# for an object with two attributes (name, location)
|
72
|
-
lookup [:name, :location], using
|
72
|
+
lookup [:name, :location], using: :employee_id
|
73
|
+
|
74
|
+
# will lookup "stars/#{employee_id}" from the store
|
75
|
+
# for an object with two attributes (name, location) and expose name as nome
|
76
|
+
lookup [:name, :age], using: :id, as: {name: 'nome'}
|
73
77
|
|
74
78
|
```
|
75
79
|
|
@@ -186,3 +190,17 @@ it 'should be capable to deep lookup and inject' do
|
|
186
190
|
end
|
187
191
|
```
|
188
192
|
|
193
|
+
### Controlling the L2 cache
|
194
|
+
Looksist has support for an in memory L2 cache which it uses to optimize redis lookups. To disable L2 cache initialize looksists as below.
|
195
|
+
|
196
|
+
* Note that in no L2 cache mode, all lookups would go to redis and the gem would not optimize redundant lookups.
|
197
|
+
* Hash based lookups would still see optimizations which come from performing unique on keys when injecting values.
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
Looksist.configure do |looksist|
|
201
|
+
looksist.lookup_store = Redis.new(:url => ENV['REDIS_URL'], :driver => :hiredis)
|
202
|
+
looksist.driver = Looksist::Serializers::Her
|
203
|
+
looksist.l2_cache = :no_cache
|
204
|
+
end
|
205
|
+
|
206
|
+
```
|
data/lib/looksist/core.rb
CHANGED
@@ -42,7 +42,12 @@ module Looksist
|
|
42
42
|
|
43
43
|
|
44
44
|
def as_json(opts)
|
45
|
-
|
45
|
+
parent_lookups = {}
|
46
|
+
class_lookups = self.class.lookup_attributes || {}
|
47
|
+
if self.class.superclass.respond_to?(:lookup_attributes)
|
48
|
+
parent_lookups = self.class.superclass.lookup_attributes || {}
|
49
|
+
end
|
50
|
+
Looksist.driver.json_opts(self, class_lookups.merge(parent_lookups), opts)
|
46
51
|
end
|
47
52
|
|
48
53
|
end
|
@@ -51,7 +56,7 @@ module Looksist
|
|
51
56
|
class Her
|
52
57
|
class << self
|
53
58
|
def json_opts(obj, lookup_attributes, _)
|
54
|
-
lookup_attributes
|
59
|
+
lookup_attributes ||= {}
|
55
60
|
other_attributes = lookup_attributes.keys.each_with_object({}) do |a, acc|
|
56
61
|
using = lookup_attributes[a]
|
57
62
|
acc[a] = obj.send(a) if obj.respond_to?(using)
|
@@ -34,12 +34,12 @@ module Looksist
|
|
34
34
|
def find_all(entity, ids)
|
35
35
|
raise 'Buffer overflow! Increase buffer size' if ids.length > @buffer_size && @buffer_size != 0
|
36
36
|
keys = ids.collect { |id| redis_key(entity, id) }
|
37
|
-
missed_keys = cache_op(
|
37
|
+
missed_keys = cache_op(proc { keys.uniq }) { (keys - @cache.keys).uniq }
|
38
38
|
unless missed_keys.empty?
|
39
39
|
response = Hash[*missed_keys.zip(@client.mget *missed_keys).flatten]
|
40
40
|
cache_op { @cache.merge!(response) }
|
41
41
|
end
|
42
|
-
(cache_op(
|
42
|
+
(cache_op(proc { keys.collect {|k| response[k] } }) { @cache.mslice(keys) })
|
43
43
|
end
|
44
44
|
|
45
45
|
def cache_op(computed = nil)
|
@@ -58,15 +58,13 @@ module Looksist
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def fetch(key, &block)
|
61
|
-
(cache_op(
|
61
|
+
(cache_op(proc { block.call }) { @cache[key] ||= block.call })
|
62
62
|
end
|
63
63
|
|
64
64
|
def redis_key(entity, id)
|
65
65
|
"#{entity.pluralize}/#{id}"
|
66
66
|
end
|
67
67
|
|
68
|
-
|
69
|
-
Proc.new
|
70
|
-
end
|
68
|
+
|
71
69
|
end
|
72
70
|
end
|
data/lib/looksist/version.rb
CHANGED
@@ -60,6 +60,51 @@ describe Looksist do
|
|
60
60
|
expect(e.to_json).to eq("{\"nome\":\"Rajini\",\"age\":16,\"id\":1}")
|
61
61
|
end
|
62
62
|
end
|
63
|
+
|
64
|
+
context 'Look up support in case of Inheritance' do
|
65
|
+
it 'should also get parent class lookups during to json conversion' do
|
66
|
+
|
67
|
+
module InheritedLookUp
|
68
|
+
class Employee
|
69
|
+
include Her::Model
|
70
|
+
use_api TEST_API
|
71
|
+
include Looksist
|
72
|
+
attr_accessor :id
|
73
|
+
attr_accessor :department_id
|
74
|
+
lookup :name, using: :id, bucket_name: 'employees'
|
75
|
+
|
76
|
+
def initialize(id, department_id)
|
77
|
+
@id = id
|
78
|
+
@department_id = department_id
|
79
|
+
end
|
80
|
+
|
81
|
+
def as_json(opts)
|
82
|
+
super(opts).merge(id: @id)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class Manager < Employee
|
87
|
+
lookup :name, using: :department_id, as: {name: 'department_name'}
|
88
|
+
|
89
|
+
def is_manager?
|
90
|
+
true
|
91
|
+
end
|
92
|
+
|
93
|
+
def as_json(opts)
|
94
|
+
super(opts).merge(is_manager: is_manager?)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
expect(@mock).to receive(:get).once.with('employees/1').and_return('SuperStar')
|
100
|
+
expect(@mock).to receive(:get).once.with('departments/2').and_return('Kollywood')
|
101
|
+
|
102
|
+
e = InheritedLookUp::Manager.new(1, 2)
|
103
|
+
|
104
|
+
expect(e.to_json).to eq("{\"department_name\":\"Kollywood\",\"name\":\"SuperStar\",\"id\":1,\"is_manager\":true}")
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
63
108
|
end
|
64
109
|
context 'with l2 cache' do
|
65
110
|
before(:each) do
|
@@ -127,7 +172,7 @@ describe Looksist do
|
|
127
172
|
end
|
128
173
|
end
|
129
174
|
end
|
130
|
-
expect(@mock).to receive(:get).never.with('
|
175
|
+
expect(@mock).to receive(:get).never.with('employees/1')
|
131
176
|
e = TolerantLookUp::Employee.new
|
132
177
|
expect(e.to_json).to eq('{}')
|
133
178
|
end
|
@@ -291,5 +336,6 @@ describe Looksist do
|
|
291
336
|
employee_second_instance.name
|
292
337
|
end
|
293
338
|
end
|
339
|
+
|
294
340
|
end
|
295
341
|
end
|