transproc 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +14 -0
- data/lib/transproc/all.rb +0 -1
- data/lib/transproc/array.rb +1 -1
- data/lib/transproc/class.rb +5 -5
- data/lib/transproc/hash.rb +34 -1
- data/lib/transproc/version.rb +1 -1
- data/rakelib/mutant.rake +1 -1
- data/spec/unit/array_transformations_spec.rb +3 -4
- data/spec/unit/hash_transformations_spec.rb +54 -0
- data/spec/unit/transproc_spec.rb +3 -3
- metadata +2 -3
- data/lib/transproc/object.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8df075878909a89bb9304ac442044a20d768860b
|
4
|
+
data.tar.gz: 2ebde290094e7cf302891f536c043af3c933f6b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5baa7f8034bb8e727c1a0f8870a4f5e9f5ec9c9d4f04ad39400572473fa543aca6ab6540ba6658a75570acf068010f0637102ac973772e349cc8a59949c602d8
|
7
|
+
data.tar.gz: 86a696d76233333a933bec5fd4ac83052cc4ce04bab8ae9bb9c11204caae818fa9020392846ff7a948b3c30fdaaa28257cad8448cf46a0d90a3a166b26b4962c
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
## v0.2.2 2015-05-22
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* `:fold` folds array of tuples into array of values from a specified key of the hash (nepalez)
|
6
|
+
|
7
|
+
### Internal
|
8
|
+
|
9
|
+
* Fixed some Rubocop warnings (nepalez)
|
10
|
+
* Changed Rubocop `RegexpLiteral` from `MaxSlashes: 0` (not supported) to
|
11
|
+
`EnforcedStyle: percent_r` (nepalez)
|
12
|
+
|
13
|
+
[Compare v0.2.1...v0.2.2](https://github.com/solnic/transproc/compare/v0.2.1...v0.2.2)
|
14
|
+
|
1
15
|
## v0.2.1 2015-05-17
|
2
16
|
|
3
17
|
### Added
|
data/lib/transproc/all.rb
CHANGED
data/lib/transproc/array.rb
CHANGED
data/lib/transproc/class.rb
CHANGED
@@ -6,9 +6,9 @@ module Transproc
|
|
6
6
|
#
|
7
7
|
# include Transproc::Helper
|
8
8
|
#
|
9
|
-
# fn = t(:constructor_inject,
|
9
|
+
# fn = t(:constructor_inject, Struct)
|
10
10
|
#
|
11
|
-
# fn[
|
11
|
+
# fn['User', :name, :age]
|
12
12
|
# # => Struct::User
|
13
13
|
#
|
14
14
|
# @api public
|
@@ -18,12 +18,12 @@ module Transproc
|
|
18
18
|
# Inject given arguments into the constructor of the class
|
19
19
|
#
|
20
20
|
# @example
|
21
|
-
# Transproct(:constructor_inject, 'User', :name, :age
|
21
|
+
# Transproct(:constructor_inject, Struct)['User', :name, :age]
|
22
22
|
# # => Struct::User
|
23
23
|
#
|
24
|
-
# @param [
|
24
|
+
# @param [*Mixed] A list of arguments to inject
|
25
25
|
#
|
26
|
-
# @return [
|
26
|
+
# @return [Object] An instance of the given klass
|
27
27
|
#
|
28
28
|
# @api public
|
29
29
|
def constructor_inject(*args, klass)
|
data/lib/transproc/hash.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'transproc/array'
|
1
2
|
require 'transproc/coercions'
|
2
3
|
|
3
4
|
module Transproc
|
@@ -151,7 +152,7 @@ module Transproc
|
|
151
152
|
#
|
152
153
|
# @api public
|
153
154
|
def reject_keys!(hash, keys)
|
154
|
-
hash.reject { |k,_| keys.include?(k) }
|
155
|
+
hash.reject { |k, _| keys.include?(k) }
|
155
156
|
end
|
156
157
|
|
157
158
|
# Rejects specified keys from a hash
|
@@ -280,5 +281,37 @@ module Transproc
|
|
280
281
|
|
281
282
|
hash
|
282
283
|
end
|
284
|
+
|
285
|
+
# Folds array of tuples to array of values from a specified key
|
286
|
+
#
|
287
|
+
# The second argument defines the key to fold tuples by
|
288
|
+
#
|
289
|
+
# @example
|
290
|
+
# source = {
|
291
|
+
# name: "Jane",
|
292
|
+
# tasks: [{ title: "be nice", priority: 1 }, { title: "sleep well" }]
|
293
|
+
# }
|
294
|
+
# Transproc(:fold, :tasks, :title)[source]
|
295
|
+
# # => { name: "Jane", tasks: ["be nice", "sleep well"] }
|
296
|
+
# Transproc(:fold, :tasks, :priority)[source]
|
297
|
+
# # => { name: "Jane", tasks: [1, nil] }
|
298
|
+
#
|
299
|
+
# @param [Hash]
|
300
|
+
#
|
301
|
+
# @return [Hash]
|
302
|
+
#
|
303
|
+
# @api public
|
304
|
+
def fold(hash, key, tuple_key)
|
305
|
+
fold!(hash.dup, key, tuple_key)
|
306
|
+
end
|
307
|
+
|
308
|
+
# Same as `:fold` but mutates the hash
|
309
|
+
#
|
310
|
+
# @see HashTransformations.fold
|
311
|
+
#
|
312
|
+
# @api public
|
313
|
+
def fold!(hash, key, tuple_key)
|
314
|
+
hash.merge!(key => ArrayTransformations.extract_key(hash[key], tuple_key))
|
315
|
+
end
|
283
316
|
end
|
284
317
|
end
|
data/lib/transproc/version.rb
CHANGED
data/rakelib/mutant.rake
CHANGED
@@ -32,8 +32,7 @@ describe Transproc::ArrayTransformations do
|
|
32
32
|
|
33
33
|
output = ['Alice', 'Bob', nil]
|
34
34
|
|
35
|
-
extract_key[input]
|
36
|
-
|
35
|
+
expect(extract_key[input]).to eql(output)
|
37
36
|
expect(input).to eql(output)
|
38
37
|
end
|
39
38
|
end
|
@@ -147,10 +146,10 @@ describe Transproc::ArrayTransformations do
|
|
147
146
|
[
|
148
147
|
{ name: 'Jane', email: 'jane@doe.org', tasks: [
|
149
148
|
{ user: 'Jane', title: 'One', tags: [{ task: 'One', tag: 'red' }] },
|
150
|
-
{ user: 'Jane', title: 'Two', tags: [] }
|
149
|
+
{ user: 'Jane', title: 'Two', tags: [] }]
|
151
150
|
},
|
152
151
|
{ name: 'Joe', email: 'joe@doe.org', tasks: [
|
153
|
-
{ user: 'Joe', title: 'Three', tags: [{ task: 'Three', tag: 'blue' }] }
|
152
|
+
{ user: 'Joe', title: 'Three', tags: [{ task: 'Three', tag: 'blue' }] }]
|
154
153
|
}
|
155
154
|
]
|
156
155
|
end
|
@@ -300,4 +300,58 @@ describe Transproc::HashTransformations do
|
|
300
300
|
expect(input).to eql(name: 'Jane', email: 'jane@doe.org', age: 21)
|
301
301
|
end
|
302
302
|
end
|
303
|
+
|
304
|
+
describe '.fold' do
|
305
|
+
let(:input) do
|
306
|
+
{
|
307
|
+
name: 'Jane',
|
308
|
+
tasks: [{ title: 'be nice', priority: 1 }, { title: 'sleep well' }]
|
309
|
+
}
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'returns an updated hash with folded key' do
|
313
|
+
fold = t(:fold, :tasks, :title)
|
314
|
+
|
315
|
+
output = { name: 'Jane', tasks: ['be nice', 'sleep well'] }
|
316
|
+
|
317
|
+
expect { fold[input] }.not_to change { input }
|
318
|
+
expect(fold[input]).to eq output
|
319
|
+
end
|
320
|
+
|
321
|
+
it 'does not compact results' do
|
322
|
+
fold = t(:fold, :tasks, :priority)
|
323
|
+
|
324
|
+
output = { name: 'Jane', tasks: [1, nil] }
|
325
|
+
|
326
|
+
expect { fold[input] }.not_to change { input }
|
327
|
+
expect(fold[input]).to eql output
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
describe '.fold!' do
|
332
|
+
let(:input) do
|
333
|
+
{
|
334
|
+
name: 'Jane',
|
335
|
+
tasks: [{ title: 'be nice', priority: 1 }, { title: 'sleep well' }]
|
336
|
+
}
|
337
|
+
end
|
338
|
+
|
339
|
+
it 'returns an updated hash with folded key' do
|
340
|
+
fold = t(:fold!, :tasks, :title)
|
341
|
+
|
342
|
+
output = { name: 'Jane', tasks: ['be nice', 'sleep well'] }
|
343
|
+
|
344
|
+
expect(fold[input]).to eql output
|
345
|
+
expect(input).to eql output
|
346
|
+
end
|
347
|
+
|
348
|
+
it 'does not compact results' do
|
349
|
+
fold = t(:fold!, :tasks, :priority)
|
350
|
+
|
351
|
+
output = { name: 'Jane', tasks: [1, nil] }
|
352
|
+
|
353
|
+
expect(fold[input]).to eql output
|
354
|
+
expect(input).to eql output
|
355
|
+
end
|
356
|
+
end
|
303
357
|
end
|
data/spec/unit/transproc_spec.rb
CHANGED
@@ -52,11 +52,11 @@ describe Transproc do
|
|
52
52
|
|
53
53
|
describe 'handling malformed input' do
|
54
54
|
it 'raises a Transproc::MalformedInputError' do
|
55
|
-
Transproc.register(:im_dangerous, ->(){
|
56
|
-
raise ArgumentError
|
55
|
+
Transproc.register(:im_dangerous, ->() {
|
56
|
+
raise ArgumentError, 'sorry, you got some bad apples in your input'
|
57
57
|
})
|
58
58
|
|
59
|
-
expect{
|
59
|
+
expect {
|
60
60
|
Transproc(:im_dangerous)[hello: 'world']
|
61
61
|
}.to raise_error(Transproc::MalformedInputError)
|
62
62
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transproc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,7 +81,6 @@ files:
|
|
81
81
|
- lib/transproc/error.rb
|
82
82
|
- lib/transproc/function.rb
|
83
83
|
- lib/transproc/hash.rb
|
84
|
-
- lib/transproc/object.rb
|
85
84
|
- lib/transproc/recursion.rb
|
86
85
|
- lib/transproc/version.rb
|
87
86
|
- rakelib/mutant.rake
|
data/lib/transproc/object.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
module Transproc
|
2
|
-
# Transformation functions for Objects
|
3
|
-
#
|
4
|
-
# @example
|
5
|
-
# require 'transproc/object'
|
6
|
-
#
|
7
|
-
# include Transproc::Helper
|
8
|
-
#
|
9
|
-
# fn = t(:set_ivars, { name: 'Jane', age: 25 })
|
10
|
-
#
|
11
|
-
# fn[Object.new]
|
12
|
-
# # => #<Object:0x007f73afe7d6f8 @name="Jane", @age=25>
|
13
|
-
#
|
14
|
-
# @api public
|
15
|
-
module ObjectTransformations
|
16
|
-
extend Functions
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|