looksist 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +134 -4
- data/lib/looksist/hashed.rb +10 -8
- data/lib/looksist/version.rb +1 -1
- data/looksist.gemspec +2 -2
- data/spec/hashed_spec.rb +47 -17
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 548c83e23d27f194d2d8f18a525def021867efe1
|
4
|
+
data.tar.gz: 6f26ee73643662a541bb3f2b7ac0359d964092e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 906eaab20e3069cb554ca7b5fbd82d082a8e9cb2ebc893aef1ef737bf21f57bd597e2ddd6e65acf5f015832c1acaa50bb4b495235420147a3e272755baaf8d96
|
7
|
+
data.tar.gz: e9ad61712df22cba869456e71a30612d6f0d03ea5375e6ba601884e9260cc396e27475c17458277701ab7e44c2b0c58e8a6e7301a17df2be331eba928f12a2ca
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Looksist
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/jpsimonroy/
|
3
|
+
[![Build Status](https://travis-ci.org/jpsimonroy/looksist.png?branch=master)](https://travis-ci.org/jpsimonroy/looksist)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/looksist.svg)](http://badge.fury.io/rb/looksist)
|
4
5
|
|
5
6
|
looksist (adj) - forming positive prejudices based on appearances
|
6
7
|
|
@@ -22,6 +23,8 @@ Or install it yourself as:
|
|
22
23
|
|
23
24
|
## Usage
|
24
25
|
|
26
|
+
### With Object Models (Her, Active Resource or any of your choice)
|
27
|
+
|
25
28
|
* Add an initializer to configure looksist
|
26
29
|
|
27
30
|
``` ruby
|
@@ -54,11 +57,138 @@ end
|
|
54
57
|
lookup takes the following form:
|
55
58
|
|
56
59
|
``` ruby
|
57
|
-
|
60
|
+
# will lookup "employees/#{employee_id}" from the store
|
61
|
+
lookup :name, using = :employee_id
|
62
|
+
|
63
|
+
# will lookup "stars/#{employee_id}" from the store
|
64
|
+
lookup :name, using = :employee_id, bucket_name="stars"
|
65
|
+
|
66
|
+
# will lookup "stars/#{employee_id}" from the store
|
67
|
+
# for an object with two attributes (name, location)
|
68
|
+
lookup [:name, :location], using = :employee_id
|
69
|
+
|
70
|
+
```
|
71
|
+
|
72
|
+
### With Plain Hashes
|
73
|
+
|
74
|
+
* Add an initializer to configure looksist
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
redis_client ||= Redis.new(:url => (ENV['REDIS_URL'], :driver => :hiredis)
|
78
|
+
|
79
|
+
Looksist::Hashed.redis_service = Looksist::RedisService.instance do |lookup|
|
80
|
+
lookup.client = redis_client
|
81
|
+
end
|
82
|
+
|
83
|
+
```
|
58
84
|
|
59
|
-
|
85
|
+
#### Columnar Hashes
|
60
86
|
|
61
|
-
|
87
|
+
* First Level look ups
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
it 'should inject multiple attribute to an existing hash' do
|
91
|
+
class HashService
|
92
|
+
include Looksist::Hashed
|
93
|
+
|
94
|
+
def metrics
|
95
|
+
{
|
96
|
+
table: {
|
97
|
+
employee_id: [5, 6],
|
98
|
+
employer_id: [3, 4]
|
99
|
+
}
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
|
+
inject after: :metrics, at: :table,
|
104
|
+
using: :employee_id, populate: :employee_name
|
105
|
+
inject after: :metrics, at: :table,
|
106
|
+
using: :employer_id, populate: :employer_name
|
107
|
+
end
|
108
|
+
# Removed mock expectations, look at the tests for actuals
|
109
|
+
expect(HashService.new.metrics).to eq({table: {
|
110
|
+
employee_id: [5, 6],
|
111
|
+
employer_id: [3, 4],
|
112
|
+
employee_name: ['emp 5', 'emp 6'],
|
113
|
+
employer_name: ['empr 3', 'empr 4']
|
114
|
+
}})
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
```
|
119
|
+
* Inner Lookups using [JsonPath](https://github.com/joshbuddy/jsonpath)
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
it 'should inject multiple attribute to an existing deep hash' do
|
123
|
+
class EmployeeHash
|
124
|
+
include Looksist::Hashed
|
125
|
+
|
126
|
+
def metrics
|
127
|
+
{
|
128
|
+
table: {
|
129
|
+
database: {
|
130
|
+
employee_id: [15, 16],
|
131
|
+
employer_id: [13, 14]
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
end
|
136
|
+
|
137
|
+
inject after: :metrics, at: '$.table.database',
|
138
|
+
using: :employee_id, populate: :employee_name
|
139
|
+
inject after: :metrics, at: '$.table.database',
|
140
|
+
using: :employer_id, populate: :employer_name
|
141
|
+
end
|
142
|
+
|
143
|
+
# Mocks removed to keep it simple.
|
144
|
+
expect(EmployeeHash.new.metrics).to eq({table: {
|
145
|
+
database: {
|
146
|
+
employee_id: [15, 16],
|
147
|
+
employer_id: [13, 14],
|
148
|
+
employee_name: ['emp 15', 'emp 16'],
|
149
|
+
employer_name: ['empr 13', 'empr 14']
|
150
|
+
}
|
151
|
+
}})
|
152
|
+
end
|
153
|
+
```
|
154
|
+
#### Non Columnar Hashes
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
it 'should be capable to deep lookup and inject' do
|
158
|
+
class Menu
|
159
|
+
include Looksist::Hashed
|
160
|
+
|
161
|
+
def metrics
|
162
|
+
{
|
163
|
+
table: {
|
164
|
+
menu: [
|
165
|
+
{
|
166
|
+
item_id: 1
|
167
|
+
},
|
168
|
+
{
|
169
|
+
item_id: 2
|
170
|
+
}
|
171
|
+
]
|
172
|
+
}
|
173
|
+
}
|
174
|
+
end
|
175
|
+
|
176
|
+
inject after: :metrics, at: '$.table.menu',
|
177
|
+
using: :item_id, populate: :item_name
|
178
|
+
end
|
62
179
|
|
180
|
+
expect(Menu.new.metrics).to eq({
|
181
|
+
table: {
|
182
|
+
menu: [{
|
183
|
+
item_id: 1,
|
184
|
+
item_name: 'Idly'
|
185
|
+
},
|
186
|
+
{
|
187
|
+
item_id: 2,
|
188
|
+
item_name: 'Pongal'
|
189
|
+
}]
|
190
|
+
}
|
191
|
+
})
|
192
|
+
end
|
63
193
|
```
|
64
194
|
|
data/lib/looksist/hashed.rb
CHANGED
@@ -12,17 +12,18 @@ module Looksist
|
|
12
12
|
module ClassMethods
|
13
13
|
def inject(opts)
|
14
14
|
raise 'Incorrect usage' unless [:after, :using, :populate].all? { |e| opts.keys.include? e }
|
15
|
+
|
16
|
+
after = opts[:after]
|
15
17
|
@rules ||= {}
|
16
|
-
@rules[
|
17
|
-
@rules[opts[:after]] << opts
|
18
|
+
(@rules[after] ||= []) << opts
|
18
19
|
|
19
|
-
return if @rules[
|
20
|
+
return if @rules[after].length > 1
|
20
21
|
|
21
|
-
define_method("#{
|
22
|
-
hash = send("#{
|
23
|
-
self.class.instance_variable_get(:@rules)[
|
22
|
+
define_method("#{after}_with_inject") do |*args|
|
23
|
+
hash = send("#{after}_without_inject".to_sym, *args)
|
24
|
+
self.class.instance_variable_get(:@rules)[after].each do |opts|
|
24
25
|
if opts[:at].is_a? String
|
25
|
-
hash = JsonPath.for(hash.with_indifferent_access).gsub(opts[:at]) do |i|
|
26
|
+
hash = JsonPath.for(hash.with_indifferent_access).gsub!(opts[:at]) do |i|
|
26
27
|
i.is_a?(Array) ? inject_attributes_for(i, opts) : inject_attributes_at(i, opts)
|
27
28
|
end.to_hash.deep_symbolize_keys
|
28
29
|
else
|
@@ -31,7 +32,8 @@ module Looksist
|
|
31
32
|
end
|
32
33
|
hash
|
33
34
|
end
|
34
|
-
alias_method_chain
|
35
|
+
alias_method_chain after, :inject
|
36
|
+
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
data/lib/looksist/version.rb
CHANGED
data/looksist.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Lookist::VERSION
|
9
9
|
spec.authors = %w(RC Simon)
|
10
10
|
spec.email = %w(rmchandru@thoughtworks.com simonroy@thoughtworks.com)
|
11
|
-
spec.summary = %q{Redis backed lookup for your
|
12
|
-
spec.description = %q{Redis backed lookup for your
|
11
|
+
spec.summary = %q{Redis backed lookup for your models}
|
12
|
+
spec.description = %q{Redis backed lookup for your models}
|
13
13
|
spec.homepage = 'https://github.com/jpsimonroy/herdis'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
data/spec/hashed_spec.rb
CHANGED
@@ -43,22 +43,18 @@ describe Looksist::Hashed do
|
|
43
43
|
expect(@mock).to receive(:get).with('items/1').and_return(OpenStruct.new(value: 'Idly'))
|
44
44
|
expect(@mock).to receive(:get).with('items/2').and_return(OpenStruct.new(value: 'Pongal'))
|
45
45
|
|
46
|
-
expect(Menu.new.metrics).to eq(
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
]
|
59
|
-
}
|
60
|
-
}
|
61
|
-
)
|
46
|
+
expect(Menu.new.metrics).to eq({
|
47
|
+
table: {
|
48
|
+
menu: [{
|
49
|
+
item_id: 1,
|
50
|
+
item_name: 'Idly'
|
51
|
+
},
|
52
|
+
{
|
53
|
+
item_id: 2,
|
54
|
+
item_name: 'Pongal'
|
55
|
+
}]
|
56
|
+
}
|
57
|
+
})
|
62
58
|
end
|
63
59
|
|
64
60
|
xit 'should be capable to deep lookup and inject - another example' do
|
@@ -188,6 +184,41 @@ describe Looksist::Hashed do
|
|
188
184
|
end
|
189
185
|
end
|
190
186
|
|
187
|
+
it 'should inject multiple attribute to an existing deep hash' do
|
188
|
+
class EmployeeHash
|
189
|
+
include Looksist::Hashed
|
190
|
+
|
191
|
+
def metrics
|
192
|
+
{
|
193
|
+
table: {
|
194
|
+
database: {
|
195
|
+
employee_id: [15, 16],
|
196
|
+
employer_id: [13, 14]
|
197
|
+
}
|
198
|
+
}
|
199
|
+
}
|
200
|
+
end
|
201
|
+
|
202
|
+
inject after: :metrics, at: '$.table.database', using: :employee_id, populate: :employee_name
|
203
|
+
inject after: :metrics, at: '$.table.database', using: :employer_id, populate: :employer_name
|
204
|
+
end
|
205
|
+
|
206
|
+
expect(@mock).to receive(:get).with('employees/15').and_return(OpenStruct.new(value: 'emp 15'))
|
207
|
+
expect(@mock).to receive(:get).with('employees/16').and_return(OpenStruct.new(value: 'emp 16'))
|
208
|
+
|
209
|
+
expect(@mock).to receive(:get).with('employers/13').and_return(OpenStruct.new(value: 'empr 13'))
|
210
|
+
expect(@mock).to receive(:get).with('employers/14').and_return(OpenStruct.new(value: 'empr 14'))
|
211
|
+
|
212
|
+
expect(EmployeeHash.new.metrics).to eq({table: {
|
213
|
+
database: {
|
214
|
+
employee_id: [15, 16],
|
215
|
+
employer_id: [13, 14],
|
216
|
+
employee_name: ['emp 15', 'emp 16'],
|
217
|
+
employer_name: ['empr 13', 'empr 14']
|
218
|
+
}
|
219
|
+
}})
|
220
|
+
end
|
221
|
+
|
191
222
|
|
192
223
|
context 'multiple methods and injections' do
|
193
224
|
it 'should inject multiple attribute to an existing hash' do
|
@@ -231,6 +262,5 @@ describe Looksist::Hashed do
|
|
231
262
|
dc_name: ['dc 7', 'dc 8']
|
232
263
|
}})
|
233
264
|
end
|
234
|
-
|
235
265
|
end
|
236
266
|
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.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- RC
|
@@ -151,7 +151,7 @@ dependencies:
|
|
151
151
|
- - "~>"
|
152
152
|
- !ruby/object:Gem::Version
|
153
153
|
version: 0.5.6
|
154
|
-
description: Redis backed lookup for your
|
154
|
+
description: Redis backed lookup for your models
|
155
155
|
email:
|
156
156
|
- rmchandru@thoughtworks.com
|
157
157
|
- simonroy@thoughtworks.com
|
@@ -201,7 +201,7 @@ rubyforge_project:
|
|
201
201
|
rubygems_version: 2.2.2
|
202
202
|
signing_key:
|
203
203
|
specification_version: 4
|
204
|
-
summary: Redis backed lookup for your
|
204
|
+
summary: Redis backed lookup for your models
|
205
205
|
test_files:
|
206
206
|
- spec/hashed_spec.rb
|
207
207
|
- spec/looksist_spec.rb
|