looksist 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +58 -0
- data/lib/looksist/hashed.rb +9 -5
- data/lib/looksist/version.rb +1 -1
- data/spec/looksist/hashed_spec.rb +19 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4435c44a529b1e8c3d6a4aa5d30dc607bee594d6
|
4
|
+
data.tar.gz: 742014189b7c80b9e461fc7e83a048414160a009
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 031b61bef8712170f79e6012f70c91f5942bd8661bbc0abc447bad35b07cb94af3e24dc45433c364d27e5b3ae2f28dea5f55443d7c9edf943ab82b0ac0f94e89
|
7
|
+
data.tar.gz: 47c2b8d8cbef46989cd8abd36131116656849cf317149901b6a97936ab5e1b12e183b0d7da1c6bbe643b1188ea5fe1bcdeb8d81f2b0064068a883e06cc1b8719
|
data/README.md
CHANGED
@@ -146,6 +146,27 @@ it 'should inject multiple attribute to an existing hash' do
|
|
146
146
|
end
|
147
147
|
|
148
148
|
```
|
149
|
+
|
150
|
+
#### Plain Hash
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
it 'should inject single attribute into a plain hash' do
|
154
|
+
class FirstLevelHash
|
155
|
+
include Looksist
|
156
|
+
|
157
|
+
def metrics
|
158
|
+
{employee_id: 5}
|
159
|
+
end
|
160
|
+
|
161
|
+
inject after: :metrics, using: :employee_id, populate: :employee_name
|
162
|
+
end
|
163
|
+
# Removed mock expectations, look at the tests for actuals
|
164
|
+
expect(FirstLevelHash.new.metrics).to eq({employeed_id: 5, employee_name: 'Emp 5'})
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
```
|
169
|
+
|
149
170
|
* Inner Lookups using [JsonPath](https://github.com/joshbuddy/jsonpath)
|
150
171
|
|
151
172
|
```ruby
|
@@ -181,6 +202,43 @@ it 'should inject multiple attribute to an existing deep hash' do
|
|
181
202
|
}})
|
182
203
|
end
|
183
204
|
```
|
205
|
+
|
206
|
+
* Inner Lookups using [JsonPath](https://github.com/joshbuddy/jsonpath)
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
it 'should inject multiple attribute to an existing deep hash for class methods' do
|
210
|
+
class EmployeeHash
|
211
|
+
include Looksist
|
212
|
+
|
213
|
+
def self.metrics
|
214
|
+
{
|
215
|
+
table: {
|
216
|
+
database: {
|
217
|
+
employee_id: [15, 16],
|
218
|
+
employer_id: [13, 14]
|
219
|
+
}
|
220
|
+
}
|
221
|
+
}
|
222
|
+
end
|
223
|
+
|
224
|
+
class_inject after: :metrics, at: '$.table.database',
|
225
|
+
using: :employee_id, populate: :employee_name
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
# Mocks removed to keep it simple.
|
230
|
+
expect(EmployeeHash.metrics).to eq({table: {
|
231
|
+
database: {
|
232
|
+
employee_id: [15, 16],
|
233
|
+
employer_id: [13, 14],
|
234
|
+
employee_name: ['emp 15', 'emp 16'],
|
235
|
+
employer_name: ['empr 13', 'empr 14']
|
236
|
+
}
|
237
|
+
}})
|
238
|
+
end
|
239
|
+
|
240
|
+
```
|
241
|
+
|
184
242
|
#### Non Columnar Hashes
|
185
243
|
|
186
244
|
```ruby
|
data/lib/looksist/hashed.rb
CHANGED
@@ -18,7 +18,7 @@ module Looksist
|
|
18
18
|
define_method("#{after}_with_inject") do |*args|
|
19
19
|
hash = send("#{after}_without_inject".to_sym, *args)
|
20
20
|
self.class.instance_variable_get(:@rules)[after].each do |opts|
|
21
|
-
if opts[:at].is_a? String
|
21
|
+
if opts[:at].nil? or opts[:at].is_a? String
|
22
22
|
hash = self.class.update_using_json_path(hash, opts)
|
23
23
|
else
|
24
24
|
self.class.inject_attributes_at(hash[opts[:at]], opts)
|
@@ -41,7 +41,7 @@ module Looksist
|
|
41
41
|
define_singleton_method("#{after}_with_inject") do |*args|
|
42
42
|
hash = send("#{after}_without_inject".to_sym, *args)
|
43
43
|
@rules[after].each do |opts|
|
44
|
-
if opts[:at].is_a? String
|
44
|
+
if opts[:at].nil? or opts[:at].is_a? String
|
45
45
|
hash = update_using_json_path(hash, opts)
|
46
46
|
else
|
47
47
|
inject_attributes_at(hash[opts[:at]], opts)
|
@@ -65,10 +65,14 @@ module Looksist
|
|
65
65
|
|
66
66
|
def update_using_json_path(hash, opts)
|
67
67
|
if hash.is_a?(Hash)
|
68
|
+
if opts[:at].present?
|
69
|
+
JsonPath.for(hash.with_indifferent_access).gsub!(opts[:at]) do |i|
|
70
|
+
i.is_a?(Array) ? inject_attributes_for(i, opts) : inject_attributes_at(i, opts) unless (i.nil? or i.empty?)
|
71
|
+
i
|
72
|
+
end
|
73
|
+
else
|
74
|
+
inject_attributes_at(hash, opts)
|
68
75
|
|
69
|
-
JsonPath.for(hash.with_indifferent_access).gsub!(opts[:at]) do |i|
|
70
|
-
i.is_a?(Array) ? inject_attributes_for(i, opts) : inject_attributes_at(i, opts) unless (i.nil? or i.empty?)
|
71
|
-
i
|
72
76
|
end.to_hash.deep_symbolize_keys
|
73
77
|
else
|
74
78
|
inject_attributes_for(hash, opts)
|
data/lib/looksist/version.rb
CHANGED
@@ -372,6 +372,25 @@ describe Looksist::Hashed do
|
|
372
372
|
}})
|
373
373
|
end
|
374
374
|
|
375
|
+
it 'should work for first level of substitution' do
|
376
|
+
class FirstLevelHash
|
377
|
+
include Looksist
|
378
|
+
|
379
|
+
def metrics
|
380
|
+
{employee_id: 10}
|
381
|
+
end
|
382
|
+
|
383
|
+
inject after: :metrics, using: :employee_id, populate: :employee_name
|
384
|
+
end
|
385
|
+
|
386
|
+
expect(@mock).to receive(:get).with('employees/10').and_return('emp 1')
|
387
|
+
|
388
|
+
expect(FirstLevelHash.new.metrics).to eq({
|
389
|
+
employee_id: 10,
|
390
|
+
employee_name: 'emp 1'
|
391
|
+
})
|
392
|
+
end
|
393
|
+
|
375
394
|
it 'should work for array of hashes' do
|
376
395
|
class ArrayOfHashes
|
377
396
|
include Looksist
|
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.3.
|
4
|
+
version: 0.3.1
|
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-
|
12
|
+
date: 2014-10-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|