looksist 0.2.1 → 0.2.2

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: 6104d9b264565171b9c36c00f2ed77c92f032f1a
4
- data.tar.gz: d8b49f214f2e2a06a107597d14f430064f451c13
3
+ metadata.gz: b167f1cd856e78371a8a08eb7a16c9b2b05146fa
4
+ data.tar.gz: b77389994a04d4527386ed0a9d30594a6e721599
5
5
  SHA512:
6
- metadata.gz: 00c77002c2b385a4dcebe00cf88b63beab0e952c4561081c712cf2e556c39f060b716637d7bc590f0c5a80638124f5f3a67782a9167291f3d2a31b8060cbc13b
7
- data.tar.gz: e5fb28133d5630f8b2b0f1d52a8f9bdb7945d81876f4994204cb68d280f58060268e8053500b54a8c641994fa49cd6c6fbb24ece6cf996445260f41cc0760e71
6
+ metadata.gz: 388347f3c8a1d053f9c0a82a19d5561dcb604088198637792e264719a86aed7aa6839245827fcf8d4e9ffee2f91f216cb43919c08da1a60837f7afe3b9b07a40
7
+ data.tar.gz: 04893bbe31f866211bde64c6b84d1f394285aeccdec1ee6b39cdfa051a8b2b296def0aceed9dffaae4e6d671f50e2baa9bde9e7a57b1958f1691bcf113a6affc
@@ -1,7 +1,7 @@
1
1
  class Employee
2
2
  include Looksist
3
3
  attr_accessor :employee_id
4
- lookup :name, using = :employee_id
4
+ lookup :name, using: :employee_id
5
5
 
6
6
  def initialize(id)
7
7
  @employee_id = id
data/lib/looksist/core.rb CHANGED
@@ -4,28 +4,39 @@ module Looksist
4
4
  include Looksist::Common
5
5
 
6
6
  module ClassMethods
7
- def lookup(what, using, bucket = using)
7
+ def lookup(what, opts)
8
+ unless opts.keys.all? { |k| [:using, :bucket_name, :as].include? k }
9
+ raise 'Incorrect usage: Invalid parameter specified'
10
+ end
11
+ using, bucket_name, as = opts[:using], opts[:bucket_name] || opts[:using], opts[:as]
8
12
  if what.is_a? Array
9
- setup_composite_lookup(bucket, using, what)
13
+ setup_composite_lookup(bucket_name, using, what, as)
10
14
  else
11
- self.lookup_attributes << what.to_sym
12
- define_method(what) do
13
- Looksist.redis_service.send("#{__entity__(bucket)}_for", self.send(using).try(:to_s))
15
+ alias_what = find_alias(as, what)
16
+ self.lookup_attributes << alias_what
17
+ define_method(alias_what) do
18
+ Looksist.redis_service.send("#{__entity__(bucket_name)}_for", self.send(using).try(:to_s))
14
19
  end
15
20
  end
16
21
  end
17
22
 
18
23
  private
19
- def setup_composite_lookup(bucket, using, what)
24
+ def setup_composite_lookup(bucket, using, what, as)
20
25
  what.each do |method_name|
21
- define_method(method_name) do
26
+ alias_method_name = find_alias(as, method_name)
27
+ define_method(alias_method_name) do
22
28
  JSON.parse(Looksist.redis_service.send("#{__entity__(bucket)}_for", self.send(using).try(:to_s)) || '{}')[method_name.to_s]
23
29
  end
24
- self.lookup_attributes << method_name
30
+ self.lookup_attributes << alias_method_name
25
31
  end
26
32
  end
33
+
34
+ def find_alias(as_map, what)
35
+ (as_map and as_map.has_key?(what)) ? as_map[what].to_sym : what
36
+ end
27
37
  end
28
38
 
39
+
29
40
  def as_json(opts)
30
41
  Looksist.driver.json_opts(self, opts)
31
42
  end
@@ -1,3 +1,3 @@
1
1
  module Lookist
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
data/looksist.gemspec CHANGED
@@ -35,5 +35,4 @@ Gem::Specification.new do |spec|
35
35
 
36
36
  spec.add_runtime_dependency 'jsonpath', '~> 0.5.6'
37
37
 
38
-
39
38
  end
@@ -18,7 +18,7 @@ describe Looksist do
18
18
  use_api TEST_API
19
19
  include Looksist
20
20
 
21
- lookup :name, using = :employee_id
21
+ lookup :name, using: :employee_id
22
22
 
23
23
  def as_json(opts)
24
24
  super(opts).merge(another_attr: 'Hello World')
@@ -38,7 +38,7 @@ describe Looksist do
38
38
  class Employee
39
39
  include Looksist
40
40
  attr_accessor :id
41
- lookup :name, using = :id, bucket_name = 'employees'
41
+ lookup :name, using: :id, bucket_name: 'employees'
42
42
 
43
43
  def initialize(id)
44
44
  @id = id
@@ -51,6 +51,57 @@ describe Looksist do
51
51
  end
52
52
  end
53
53
 
54
+ context 'Alias support for lookup' do
55
+ it 'should fetch attributes and use the alias specified in the api' do
56
+ module AliasLookup
57
+ class Employee
58
+ include Her::Model
59
+ use_api TEST_API
60
+ include Looksist
61
+ attr_accessor :id
62
+ lookup :name, using: :id, bucket_name: 'employees', as: {name: 'nome'}
63
+
64
+ def initialize(id)
65
+ @id = id
66
+ end
67
+
68
+ def as_json(opts)
69
+ super(opts).merge(id: @id)
70
+ end
71
+ end
72
+ end
73
+ expect(@mock).to receive(:get).once.with('employees/1').and_return('Rajini')
74
+ e = AliasLookup::Employee.new(1)
75
+ expect(e.nome).to eq('Rajini')
76
+ expect(e.to_json).to eq("{\"nome\":\"Rajini\",\"id\":1}")
77
+ end
78
+
79
+ it 'should fetch attributes and use the alias for specific attributes in the api' do
80
+ module AliasSpecificLookup
81
+ class Employee
82
+ include Her::Model
83
+ use_api TEST_API
84
+ include Looksist
85
+ attr_accessor :id
86
+ lookup [:name, :age], using: :id, as: {name: 'nome'}
87
+
88
+ def initialize(id)
89
+ @id = id
90
+ end
91
+
92
+ def as_json(opts)
93
+ super(opts).merge(id: @id)
94
+ end
95
+ end
96
+ end
97
+ expect(@mock).to receive(:get).once.with('ids/1').and_return({name: 'Rajini', age: 16}.to_json)
98
+ e = AliasSpecificLookup::Employee.new(1)
99
+ expect(e.nome).to eq('Rajini')
100
+ expect(e.age).to eq(16)
101
+ expect(e.to_json).to eq("{\"nome\":\"Rajini\",\"age\":16,\"id\":1}")
102
+ end
103
+ end
104
+
54
105
  context 'Lazy Evaluation' do
55
106
  module LazyEval
56
107
  class HerEmployee
@@ -58,7 +109,7 @@ describe Looksist do
58
109
  use_api TEST_API
59
110
  include Looksist
60
111
 
61
- lookup :name, using = :employee_id
112
+ lookup :name, using: :employee_id
62
113
 
63
114
  def as_json(opts)
64
115
  super(opts).merge(another_attr: 'Hello World')
@@ -67,7 +118,7 @@ describe Looksist do
67
118
  class Employee
68
119
  include Looksist
69
120
  attr_accessor :id
70
- lookup :name, using = :id, bucket_name = 'employees'
121
+ lookup :name, using: :id, bucket_name: 'employees'
71
122
 
72
123
  def initialize(id)
73
124
  @id = id
@@ -87,8 +138,8 @@ describe Looksist do
87
138
  class Employee
88
139
  include Looksist
89
140
  attr_accessor :id, :employee_id
90
- lookup :name, using= :id
91
- lookup :unavailable, using= :employee_id
141
+ lookup :name, using: :id
142
+ lookup :unavailable, using: :employee_id
92
143
 
93
144
  def initialize(id)
94
145
  @id = @employee_id = id
@@ -109,9 +160,9 @@ describe Looksist do
109
160
  include Looksist
110
161
  attr_accessor :id, :employee_id, :contact_id
111
162
 
112
- lookup [:name, :location], using=:id
113
- lookup [:age, :sex], using=:employee_id
114
- lookup [:pager, :cell], using=:contact_id
163
+ lookup [:name, :location], using: :id
164
+ lookup [:age, :sex], using: :employee_id
165
+ lookup [:pager, :cell], using: :contact_id
115
166
 
116
167
  def initialize(id)
117
168
  @contact_id = @id = @employee_id = id
@@ -140,7 +191,7 @@ describe Looksist do
140
191
  include Looksist
141
192
  attr_accessor :id
142
193
 
143
- lookup [:name, :location], using=:id
194
+ lookup [:name, :location], using: :id
144
195
 
145
196
  def initialize(id)
146
197
  @id = id
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.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - RC
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-10-26 00:00:00.000000000 Z
12
+ date: 2014-10-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler