looksist 0.1.3 → 0.1.4

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: 8526c6730f1f78436abccf895c6b9c66afdf0d40
4
- data.tar.gz: 49abb35ed9f3dfdf904694b25be5c48d21537e5d
3
+ metadata.gz: 46c6ea1a931ae0e7367b6448d23c37cb12d73cf2
4
+ data.tar.gz: 0d6ff0d198ece2ec4a3b8f4cdf1e60e7748e3ff5
5
5
  SHA512:
6
- metadata.gz: 6b15b2d989a8a5f0a71292112e5eb0abfbb120f277b6e15f0b1d38545b3c2d17e3f1583ea8985b18aba42d8f728410c5166ee2b074c1cf21d561c0f1d64ee946
7
- data.tar.gz: 541a9d243b0d3f9b1721020e5d91e8650562fe10f9774f0d7ba0bd2270d93f55458374c75c194679eedfd55a76702d0eb2eb080cbac8fe5f69d073b890ae6ba1
6
+ metadata.gz: 11993fd0f01590a1ad5cc85f64d1cda21608cc8a74f380501dd1b935aa43ce810cee112167dc36b9c3f7e176de65263ceb487386c7a1e654963af2fd01c5cdde
7
+ data.tar.gz: 4fd89a0710c78b8626886d67fc15f6455a9f1e7b8b5545e8252075c789d7958820298273820614bdc804c093edcbb34ca97d0bf3dc63ee02e6c5ef14eb578409
data/lib/looksist.rb CHANGED
@@ -1,10 +1,9 @@
1
1
  require 'looksist/version'
2
2
  require 'jsonpath'
3
- require 'json'
3
+ require 'looksist/common'
4
4
  require 'looksist/core'
5
5
  require 'looksist/redis_service'
6
6
  require 'looksist/hashed'
7
- require 'looksist/her_collection'
8
7
  require 'looksist/safe_lru_cache'
9
8
 
10
9
 
@@ -0,0 +1,7 @@
1
+ module Looksist
2
+ module Common
3
+ def entity(entity)
4
+ entity.to_s.gsub('_id', '')
5
+ end
6
+ end
7
+ end
data/lib/looksist/core.rb CHANGED
@@ -1,48 +1,23 @@
1
1
  module Looksist
2
2
  module Core
3
3
  extend ActiveSupport::Concern
4
+ include Looksist::Common
4
5
 
5
6
  module ClassMethods
6
-
7
- def bucket_name(entity_id)
8
- entity = entity_id.to_s.gsub('_id', '')
9
- entity.pluralize
10
- end
11
-
12
- def memoized(key)
13
- self.storage ||= OpenStruct.new
14
- self.storage[key] = self.storage[key] || Looksist.lookup_store.get(key)
15
- end
16
-
17
- def mmemoized(key, values)
18
- key_and_bucket = id_and_buckets.find{|h| h[:id] == key}
19
- return if key_and_bucket.nil?
20
- redis_keys = values.collect{|v| redis_key(key_and_bucket[:bucket], v)}
21
- left_keys_to_lookup = redis_keys.select{|k| self.storage[k].nil?}
22
- Looksist.lookup_store.mapped_mget(left_keys_to_lookup).each do |key, value|
23
- self.storage[key] = value
24
- end
25
-
26
- end
27
-
28
- def redis_key bucket, value
29
- [bucket, '/', value].join('')
30
- end
31
-
32
- def lookup(what, using, bucket = bucket_name(using))
7
+ def lookup(what, using, bucket = using)
33
8
  self.lookup_attributes ||= []
34
9
  self.id_and_buckets ||= []
35
10
  self.id_and_buckets << {id: using, bucket: bucket}
36
11
  if what.is_a? Array
37
12
  what.each do |method_name|
38
13
  define_method(method_name) do
39
- JSON.parse(self.class.memoized(self.class.redis_key(bucket, self.send(using).try(:to_s))) || '{}')[method_name.to_s]
14
+ JSON.parse(Looksist.redis_service.send("#{entity(bucket)}_for", self.send(using).try(:to_s)) || '{}')[method_name.to_s]
40
15
  end
41
16
  self.lookup_attributes << method_name
42
17
  end
43
18
  else
44
19
  define_method(what) do
45
- self.class.memoized(self.class.redis_key(bucket, self.send(using).try(:to_s)))
20
+ Looksist.redis_service.send("#{entity(bucket)}_for", self.send(using).try(:to_s))
46
21
  end
47
22
  self.lookup_attributes << what.to_sym
48
23
  end
@@ -4,6 +4,7 @@ require 'json'
4
4
  module Looksist
5
5
  module Hashed
6
6
  extend ActiveSupport::Concern
7
+ include Looksist::Common
7
8
 
8
9
  module ClassMethods
9
10
  def inject(opts)
@@ -38,9 +39,6 @@ module Looksist
38
39
  end
39
40
 
40
41
  private
41
- def entity(entity_id)
42
- entity_id.to_s.gsub('_id', '')
43
- end
44
42
 
45
43
  def inject_attributes_at(hash_offset, opts)
46
44
  return nil unless hash_offset
@@ -1,3 +1,3 @@
1
1
  module Lookist
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
@@ -2,10 +2,11 @@ require 'spec_helper'
2
2
 
3
3
 
4
4
  describe Looksist do
5
- before :each do
5
+ before(:each) do
6
+ @mock = {}
6
7
  Looksist.configure do |looksist|
7
- looksist.lookup_store = double('store_lookup_client')
8
- looksist.driver = Looksist::Serializers::Her
8
+ looksist.lookup_store = @mock
9
+ looksist.driver = Looksist::Serializers::Her
9
10
  end
10
11
  end
11
12
 
@@ -24,7 +25,7 @@ describe Looksist do
24
25
  end
25
26
  end
26
27
  end
27
- expect(Looksist.lookup_store).to receive(:get).with('employees/1').and_return('Employee Name')
28
+ expect(@mock).to receive(:get).once.with('employees/1').and_return('Employee Name')
28
29
  e = Her::Employee.new({employee_id: 1})
29
30
  expect(e.name).to eq('Employee Name')
30
31
  expect(e.to_json).to eq({:employee_id => 1, :name => 'Employee Name', :another_attr => 'Hello World'}.to_json)
@@ -44,7 +45,7 @@ describe Looksist do
44
45
  end
45
46
  end
46
47
  end
47
- expect(Looksist.lookup_store).to receive(:get).with('employees/1').and_return('Employee Name')
48
+ expect(@mock).to receive(:get).once.with('employees/1').and_return('Employee Name')
48
49
  e = ExplicitBucket::Employee.new(1)
49
50
  expect(e.name).to eq('Employee Name')
50
51
  end
@@ -63,7 +64,7 @@ describe Looksist do
63
64
  end
64
65
  end
65
66
  it 'should not eager evaluate' do
66
- expect(Looksist.lookup_store).to_not receive(:get)
67
+ expect(@mock).to_not receive(:get)
67
68
  LazyEval::Employee.new(1)
68
69
  end
69
70
  end
@@ -83,8 +84,8 @@ describe Looksist do
83
84
  end
84
85
  end
85
86
 
86
- expect(Looksist.lookup_store).to receive(:get).with('ids/1').and_return('Employee Name')
87
- expect(Looksist.lookup_store).to receive(:get).with('employees/1').and_return(nil)
87
+ expect(@mock).to receive(:get).once.with('ids/1').and_return('Employee Name')
88
+ expect(@mock).to receive(:get).once.with('employees/1').and_return(nil)
88
89
  e = SimpleLookup::Employee.new(1)
89
90
  expect(e.name).to eq('Employee Name')
90
91
  expect(e.unavailable).to be(nil)
@@ -94,28 +95,31 @@ describe Looksist do
94
95
  module CompositeLookup
95
96
  class Employee
96
97
  include Looksist
97
- attr_accessor :id, :employee_id
98
+ attr_accessor :id, :employee_id, :contact_id
98
99
 
99
100
  lookup [:name, :location], using=:id
100
101
  lookup [:age, :sex], using=:employee_id
102
+ lookup [:pager, :cell], using=:contact_id
101
103
 
102
104
  def initialize(id)
103
- @id = @employee_id = id
105
+ @contact_id = @id = @employee_id = id
104
106
  end
105
107
  end
106
108
  end
107
109
 
108
- expect(Looksist.lookup_store).to receive(:get).with('ids/1')
109
- .and_return({name: 'Employee Name', location: 'Chennai'}.to_json)
110
- expect(Looksist.lookup_store).to receive(:get).twice.with('employees/1')
111
- .and_return(nil)
110
+ expect(@mock).to receive(:get).once.with('ids/1').and_return({name: 'Employee Name', location: 'Chennai'}.to_json)
111
+ expect(@mock).to receive(:get).once.with('contacts/1').and_return({pager: 'pager', cell: 'cell'}.to_json)
112
+ expect(@mock).to receive(:get).twice.with('employees/1').and_return(nil)
112
113
  e = CompositeLookup::Employee.new(1)
113
114
 
114
115
  expect(e.name).to eq('Employee Name')
115
116
  expect(e.location).to eq('Chennai')
116
117
 
117
- expect(e.age).to be(nil)
118
- expect(e.sex).to be(nil)
118
+ expect(e.age).to eq(nil)
119
+ expect(e.sex).to eq(nil)
120
+
121
+ expect(e.pager).to eq('pager')
122
+ expect(e.cell).to eq('cell')
119
123
  end
120
124
  end
121
125
 
@@ -132,48 +136,14 @@ describe Looksist do
132
136
  end
133
137
  it 'should share storage between instances to improve performance' do
134
138
  employee_first_instance = Employee.new(1)
135
- expect(Looksist.lookup_store).to receive(:get).with('ids/1')
136
- .and_return({name: 'Employee Name', location: 'Chennai'}.to_json)
139
+ expect(@mock).to receive(:get).once.with('ids/1')
140
+ .and_return({name: 'Employee Name', location: 'Chennai'}.to_json)
137
141
  employee_first_instance.name
138
142
 
139
143
  employee_second_instance = Employee.new(1)
140
- expect(Looksist.lookup_store).not_to receive(:get).with('ids/1')
144
+ expect(@mock).not_to receive(:get).with('ids/1')
141
145
 
142
146
  employee_second_instance.name
143
147
  end
144
148
  end
145
-
146
- context '.id_and_buckets' do
147
- class Developer
148
- include Looksist
149
- lookup [:city], using=:city_id
150
- lookup [:role], using=:role_id
151
- end
152
- it 'should hold all the id and buckets' do
153
- expect(Developer.id_and_buckets).to eq([{id: :city_id, bucket: 'cities'}, {id: :role_id, bucket: 'roles'}])
154
- end
155
- end
156
-
157
- context '.mmemoized' do
158
- class AnotherDeveloperClass
159
- include Looksist
160
- lookup [:city], using=:city_id
161
- lookup [:role], using=:role_id
162
- end
163
-
164
- AnotherDeveloperClass.storage = OpenStruct.new
165
- AnotherDeveloperClass.storage['cities/1'] = 'Chennai'
166
- AnotherDeveloperClass.storage['cities/2'] = 'Delhi'
167
-
168
- it 'make single request for multiple values' do
169
- expect(Looksist.lookup_store).to receive(:mapped_mget).with(%w(cities/4 cities/5))
170
- .and_return({'cities/4' => 'Bangalore', 'cities/5' => 'Kolkata'})
171
- AnotherDeveloperClass.mmemoized(:city_id, [1, 4, 5])
172
-
173
- expect(AnotherDeveloperClass.storage.to_h.length).to eq(4)
174
- expect(AnotherDeveloperClass.storage['cities/5']).to eq('Kolkata')
175
- expect(AnotherDeveloperClass.storage['cities/4']).to eq('Bangalore')
176
-
177
- end
178
- end
179
149
  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.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - RC
@@ -169,9 +169,9 @@ files:
169
169
  - README.md
170
170
  - Rakefile
171
171
  - lib/looksist.rb
172
+ - lib/looksist/common.rb
172
173
  - lib/looksist/core.rb
173
174
  - lib/looksist/hashed.rb
174
- - lib/looksist/her_collection.rb
175
175
  - lib/looksist/redis_service.rb
176
176
  - lib/looksist/safe_lru_cache.rb
177
177
  - lib/looksist/version.rb
@@ -1,13 +0,0 @@
1
- module Her
2
- class Collection
3
- def load_all_meta
4
- klass = a.first.class
5
- return unless klass.respond_to?(:id_and_buckets)
6
- id_attributes = klass.id_and_buckets.collect{|h| h[:id]}
7
- id_attributes.each do |attribute|
8
- id_attribute_values = self.collect(&attribute.to_sym)
9
- klass.mmemoized(attribute, id_attribute_values)
10
- end
11
- end
12
- end
13
- end