transproc 0.1.2 → 0.1.3
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/.gitignore +1 -0
- data/.travis.yml +2 -0
- data/CHANGELOG.md +13 -0
- data/README.md +1 -1
- data/lib/transproc/all.rb +1 -0
- data/lib/transproc/array.rb +10 -12
- data/lib/transproc/composer.rb +7 -5
- data/lib/transproc/function.rb +2 -1
- data/lib/transproc/hash.rb +15 -0
- data/lib/transproc/recursion.rb +29 -0
- data/lib/transproc/version.rb +1 -1
- data/spec/integration/array_spec.rb +10 -10
- data/spec/integration/coercions_spec.rb +11 -11
- data/spec/integration/hash_spec.rb +54 -15
- data/spec/integration/recursion_spec.rb +106 -0
- data/spec/integration/transproc_spec.rb +5 -5
- data/spec/spec_helper.rb +4 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b43b407d9915f16f63dbf1a12f10c9a87228213c
|
4
|
+
data.tar.gz: a46a7614a839a50da267593eeef25acbc8db167b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ce15554b0639b3c79a0a4da686e588f01dd0de7798b89a7129c39b0a51f0f0c31b094533775b3b21f5b6b893917020f2d99aa1b84648c445617c75f3f8e5688
|
7
|
+
data.tar.gz: 104618d775a9ce9781583b2c6f4e0e61ca28107f1f4494124b41456c8442b4663a7d4ad5b353dd6641b0e885f56d5b33ee0d0a32e23ff5a6ad4c9fcf2f029e96
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
## v0.1.3 to-be-released
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* Added `hash_recursion` and `array_recursion` functions (AMHOL)
|
6
|
+
* Added `unwrap` and `unwrap!` functions (aflatter)
|
7
|
+
|
8
|
+
### Changed
|
9
|
+
|
10
|
+
* Speedup transproc `group` (splattael)
|
11
|
+
|
12
|
+
[Compare v0.1.2...v0.1.3](https://github.com/solnic/transproc/compare/v0.1.2...v0.1.3)
|
13
|
+
|
1
14
|
## v0.1.2 2015-03-14
|
2
15
|
|
3
16
|
### Changed
|
data/README.md
CHANGED
@@ -39,7 +39,7 @@ Or install it yourself as:
|
|
39
39
|
require 'transproc/all'
|
40
40
|
|
41
41
|
# compose transformation functions
|
42
|
-
transformation = Transproc(:symbolize_keys)
|
42
|
+
transformation = Transproc(:symbolize_keys) >> Transproc(:map_hash, user_name: :name))
|
43
43
|
|
44
44
|
# call the function
|
45
45
|
transformation['user_name' => 'Jane']
|
data/lib/transproc/all.rb
CHANGED
data/lib/transproc/array.rb
CHANGED
@@ -13,17 +13,15 @@ module Transproc
|
|
13
13
|
end
|
14
14
|
|
15
15
|
register(:group) do |array, key, keys|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
root.merge(key => children)
|
27
|
-
}
|
16
|
+
grouped = Hash.new { |hash, key| hash[key] = [] }
|
17
|
+
array.each do |hash|
|
18
|
+
hash = hash.dup
|
19
|
+
child = {}
|
20
|
+
keys.each { |k| child[k] = hash.delete(k) }
|
21
|
+
grouped[hash] << child
|
22
|
+
end
|
23
|
+
grouped.map do |root, children|
|
24
|
+
root.merge(key => children)
|
25
|
+
end
|
28
26
|
end
|
29
27
|
end
|
data/lib/transproc/composer.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
module Transproc
|
2
|
+
module Helper
|
3
|
+
def t(*args, &block)
|
4
|
+
Transproc(*args, &block)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
2
8
|
module Composer
|
9
|
+
include Helper
|
3
10
|
|
4
11
|
class Factory
|
5
12
|
attr_reader :fns, :default
|
@@ -19,15 +26,10 @@ module Transproc
|
|
19
26
|
end
|
20
27
|
end
|
21
28
|
|
22
|
-
def t(*args)
|
23
|
-
Transproc(*args)
|
24
|
-
end
|
25
|
-
|
26
29
|
def compose(default = nil)
|
27
30
|
factory = Factory.new(default)
|
28
31
|
yield(factory)
|
29
32
|
factory.to_fn
|
30
33
|
end
|
31
|
-
|
32
34
|
end
|
33
35
|
end
|
data/lib/transproc/function.rb
CHANGED
@@ -13,8 +13,9 @@ module Transproc
|
|
13
13
|
alias_method :[], :call
|
14
14
|
|
15
15
|
def compose(other)
|
16
|
-
self.class.new(-> *
|
16
|
+
self.class.new(-> *input { other[fn[*input]] }, args)
|
17
17
|
end
|
18
18
|
alias_method :+, :compose
|
19
|
+
alias_method :>>, :compose
|
19
20
|
end
|
20
21
|
end
|
data/lib/transproc/hash.rb
CHANGED
@@ -39,4 +39,19 @@ module Transproc
|
|
39
39
|
hash.update(root => {})
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
register(:unwrap) do |hash, root, keys|
|
44
|
+
copy = Hash[hash].merge(root => Hash[hash[root]])
|
45
|
+
Transproc(:unwrap!, root, keys)[copy]
|
46
|
+
end
|
47
|
+
|
48
|
+
register(:unwrap!) do |hash, root, keys|
|
49
|
+
if nested_hash = hash[root]
|
50
|
+
keys ||= nested_hash.keys
|
51
|
+
hash.update(Hash[keys.zip(keys.map { |key| nested_hash.delete(key) })])
|
52
|
+
hash.delete(root) if nested_hash.empty?
|
53
|
+
end
|
54
|
+
|
55
|
+
hash
|
56
|
+
end
|
42
57
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Transproc
|
2
|
+
register(:array_recursion) do |value, fn|
|
3
|
+
result = fn[value]
|
4
|
+
|
5
|
+
result.map! do |item|
|
6
|
+
if item.is_a?(::Array)
|
7
|
+
Transproc(:array_recursion, fn)[item]
|
8
|
+
else
|
9
|
+
item
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
register(:hash_recursion) do |value, fn|
|
15
|
+
result = fn[value]
|
16
|
+
|
17
|
+
result.keys.each do |key|
|
18
|
+
item = result.delete(key)
|
19
|
+
|
20
|
+
if item.is_a?(::Hash)
|
21
|
+
result[key] = Transproc(:hash_recursion, fn)[item]
|
22
|
+
else
|
23
|
+
result[key] = item
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
result
|
28
|
+
end
|
29
|
+
end
|
data/lib/transproc/version.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe 'Array transformations with Transproc' do
|
4
4
|
describe 'map_array' do
|
5
5
|
it 'applies funtions to all values' do
|
6
|
-
map =
|
6
|
+
map = t(:map_array, t(:symbolize_keys))
|
7
7
|
|
8
8
|
original = [
|
9
9
|
{ 'name' => 'Jane', 'title' => 'One' },
|
@@ -24,7 +24,7 @@ describe 'Array transformations with Transproc' do
|
|
24
24
|
|
25
25
|
describe 'map_array!' do
|
26
26
|
it 'updates array with the result of the function applied to each value' do
|
27
|
-
map =
|
27
|
+
map = t(:map_array!, t(:symbolize_keys))
|
28
28
|
|
29
29
|
input = [
|
30
30
|
{ 'name' => 'Jane', 'title' => 'One' },
|
@@ -44,7 +44,7 @@ describe 'Array transformations with Transproc' do
|
|
44
44
|
|
45
45
|
describe 'wrap' do
|
46
46
|
it 'returns a new array with wrapped hashes' do
|
47
|
-
wrap =
|
47
|
+
wrap = t(:wrap, :task, [:title])
|
48
48
|
|
49
49
|
input = [{ name: 'Jane', title: 'One' }]
|
50
50
|
output = [{ name: 'Jane', task: { title: 'One' } }]
|
@@ -54,10 +54,10 @@ describe 'Array transformations with Transproc' do
|
|
54
54
|
|
55
55
|
it 'returns a array new with deeply wrapped hashes' do
|
56
56
|
wrap =
|
57
|
-
|
57
|
+
t(
|
58
58
|
:map_array,
|
59
|
-
|
60
|
-
|
59
|
+
t(:nest, :user, [:name, :title]),
|
60
|
+
t(:map_key, :user, t(:nest, :task, [:title]))
|
61
61
|
)
|
62
62
|
|
63
63
|
input = [{ name: 'Jane', title: 'One' }]
|
@@ -69,7 +69,7 @@ describe 'Array transformations with Transproc' do
|
|
69
69
|
|
70
70
|
describe 'group' do
|
71
71
|
it 'returns a new array with grouped hashes' do
|
72
|
-
group =
|
72
|
+
group = t(:group, :tasks, [:title])
|
73
73
|
|
74
74
|
input = [{ name: 'Jane', title: 'One' }, { name: 'Jane', title: 'Two' }]
|
75
75
|
output = [{ name: 'Jane', tasks: [{ title: 'One' }, { title: 'Two' }] }]
|
@@ -80,8 +80,8 @@ describe 'Array transformations with Transproc' do
|
|
80
80
|
|
81
81
|
describe 'composition' do
|
82
82
|
it 'allows composing transformations' do
|
83
|
-
map =
|
84
|
-
group =
|
83
|
+
map = t(:map_array, t(:symbolize_keys))
|
84
|
+
group = t(:group, :tasks, [:title])
|
85
85
|
|
86
86
|
input = [
|
87
87
|
{ 'name' => 'Jane', 'title' => 'One' },
|
@@ -90,7 +90,7 @@ describe 'Array transformations with Transproc' do
|
|
90
90
|
|
91
91
|
output = [{ name: 'Jane', tasks: [{ title: 'One' }, { title: 'Two' }] }]
|
92
92
|
|
93
|
-
transformation = map
|
93
|
+
transformation = map >> group
|
94
94
|
|
95
95
|
expect(transformation[input]).to eql(output)
|
96
96
|
end
|
@@ -3,63 +3,63 @@ require 'spec_helper'
|
|
3
3
|
describe 'Transproc / Coercions' do
|
4
4
|
describe 'to_string' do
|
5
5
|
it 'turns integer into a string' do
|
6
|
-
expect(
|
6
|
+
expect(t(:to_string)[1]).to eql('1')
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
describe 'to_integer' do
|
11
11
|
it 'turns string into an integer' do
|
12
|
-
expect(
|
12
|
+
expect(t(:to_integer)['1']).to eql(1)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
describe 'to_float' do
|
17
17
|
it 'turns string into a float' do
|
18
|
-
expect(
|
18
|
+
expect(t(:to_float)['1']).to eql(1.0)
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'turns integer into a float' do
|
22
|
-
expect(
|
22
|
+
expect(t(:to_float)[1]).to eql(1.0)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
describe 'to_decimal' do
|
27
27
|
it 'turns string into a decimal' do
|
28
|
-
expect(
|
28
|
+
expect(t(:to_decimal)['1.251']).to eql(BigDecimal('1.251'))
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'turns float into a decimal' do
|
32
|
-
expect(
|
32
|
+
expect(t(:to_decimal)[1.251]).to eql(BigDecimal('1.251'))
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'turns integer into a decimal' do
|
36
|
-
expect(
|
36
|
+
expect(t(:to_decimal)[1]).to eql(BigDecimal('1.0'))
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
describe 'to_date' do
|
41
41
|
it 'turns string into a date' do
|
42
42
|
date = Date.new(1983, 11, 18)
|
43
|
-
expect(
|
43
|
+
expect(t(:to_date)['18th, November 1983']).to eql(date)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
describe 'to_time' do
|
48
48
|
it 'turns string into a time object' do
|
49
49
|
time = Time.new(2012, 1, 23, 11, 7, 7)
|
50
|
-
expect(
|
50
|
+
expect(t(:to_time)['2012-01-23 11:07:07']).to eql(time)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
describe 'to_datetime' do
|
55
55
|
it 'turns string into a date' do
|
56
56
|
datetime = DateTime.new(2012, 1, 23, 11, 7, 7)
|
57
|
-
expect(
|
57
|
+
expect(t(:to_datetime)['2012-01-23 11:07:07']).to eql(datetime)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
61
|
describe 'to_boolean' do
|
62
|
-
subject(:coercer) {
|
62
|
+
subject(:coercer) { t(:to_boolean) }
|
63
63
|
|
64
64
|
Transproc::TRUE_VALUES.each do |value|
|
65
65
|
it "turns #{value.inspect} to true" do
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe 'Hash mapping with Transproc' do
|
4
4
|
describe 'symbolize_keys' do
|
5
5
|
it 'returns a new hash with symbolized keys' do
|
6
|
-
symbolize_keys =
|
6
|
+
symbolize_keys = t(:symbolize_keys)
|
7
7
|
|
8
8
|
input = { 'foo' => 'bar' }
|
9
9
|
output = { foo: 'bar' }
|
@@ -15,7 +15,7 @@ describe 'Hash mapping with Transproc' do
|
|
15
15
|
|
16
16
|
describe 'symbolize_keys!' do
|
17
17
|
it 'returns updated hash with symbolized keys' do
|
18
|
-
symbolize_keys =
|
18
|
+
symbolize_keys = t(:symbolize_keys!)
|
19
19
|
|
20
20
|
input = { 'foo' => 'bar' }
|
21
21
|
output = { foo: 'bar' }
|
@@ -28,7 +28,7 @@ describe 'Hash mapping with Transproc' do
|
|
28
28
|
|
29
29
|
describe 'nest' do
|
30
30
|
it 'returns new hash with keys nested under a new key' do
|
31
|
-
nest =
|
31
|
+
nest = t(:nest, :baz, ['foo'])
|
32
32
|
|
33
33
|
input = { 'foo' => 'bar' }
|
34
34
|
output = { baz: { 'foo' => 'bar' } }
|
@@ -40,7 +40,7 @@ describe 'Hash mapping with Transproc' do
|
|
40
40
|
|
41
41
|
describe 'nest!' do
|
42
42
|
it 'returns new hash with keys nested under a new key' do
|
43
|
-
nest =
|
43
|
+
nest = t(:nest!, :baz, ['one', 'two', 'not-here'])
|
44
44
|
|
45
45
|
input = { 'foo' => 'bar', 'one' => nil, 'two' => false }
|
46
46
|
output = { 'foo' => 'bar', baz: { 'one' => nil, 'two' => false } }
|
@@ -51,7 +51,7 @@ describe 'Hash mapping with Transproc' do
|
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'returns new hash with an empty hash under a new key when nest-keys are missing' do
|
54
|
-
nest =
|
54
|
+
nest = t(:nest!, :baz, ['foo'])
|
55
55
|
|
56
56
|
input = { 'bar' => 'foo' }
|
57
57
|
output = { 'bar' => 'foo', baz: {} }
|
@@ -62,9 +62,48 @@ describe 'Hash mapping with Transproc' do
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
+
describe 'unwrap!' do
|
66
|
+
it 'returns updated hash with nested keys lifted to the root' do
|
67
|
+
unwrap = t(:unwrap!, 'wrapped', ['one', 'two'])
|
68
|
+
|
69
|
+
input = { 'foo' => 'bar', 'wrapped' => { 'one' => nil, 'two' => false } }
|
70
|
+
output = { 'foo' => 'bar', 'one' => nil, 'two' => false }
|
71
|
+
|
72
|
+
unwrap[input]
|
73
|
+
|
74
|
+
expect(input).to eql(output)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'lifts all keys if none are passed' do
|
78
|
+
unwrap = t(:unwrap!, 'wrapped')
|
79
|
+
|
80
|
+
input = { 'wrapped' => { 'one' => nil, 'two' => false } }
|
81
|
+
output = { 'one' => nil, 'two' => false }
|
82
|
+
|
83
|
+
unwrap[input]
|
84
|
+
|
85
|
+
expect(input).to eql(output)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe 'unwrap' do
|
90
|
+
it 'returns new hash with nested keys lifted to the root' do
|
91
|
+
unwrap = t(:unwrap, 'wrapped', ['one', 'two'])
|
92
|
+
|
93
|
+
input = {
|
94
|
+
'foo' => 'bar',
|
95
|
+
'wrapped' => { 'one' => nil, 'two' => false }
|
96
|
+
}.freeze
|
97
|
+
|
98
|
+
expect(unwrap[input]).to eql({'foo' => 'bar',
|
99
|
+
'one' => nil,
|
100
|
+
'two' => false})
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
65
104
|
describe 'map_hash' do
|
66
105
|
it 'returns a new hash with applied functions' do
|
67
|
-
map =
|
106
|
+
map = t(:map_hash, 'foo' => :foo)
|
68
107
|
|
69
108
|
input = { 'foo' => 'bar', :bar => 'baz' }
|
70
109
|
output = { foo: 'bar', bar: 'baz' }
|
@@ -76,7 +115,7 @@ describe 'Hash mapping with Transproc' do
|
|
76
115
|
|
77
116
|
describe 'map_hash!' do
|
78
117
|
it 'returns updated hash with applied functions' do
|
79
|
-
map =
|
118
|
+
map = t(:map_hash!, 'foo' => :foo)
|
80
119
|
|
81
120
|
input = { 'foo' => 'bar', :bar => 'baz' }
|
82
121
|
output = { foo: 'bar', bar: 'baz' }
|
@@ -89,7 +128,7 @@ describe 'Hash mapping with Transproc' do
|
|
89
128
|
|
90
129
|
describe 'map_key' do
|
91
130
|
it 'applies function to value under specified key' do
|
92
|
-
transformation =
|
131
|
+
transformation = t(:map_key, :user, t(:symbolize_keys))
|
93
132
|
|
94
133
|
input = { user: { 'name' => 'Jane' } }
|
95
134
|
output = { user: { name: 'Jane' } }
|
@@ -101,7 +140,7 @@ describe 'Hash mapping with Transproc' do
|
|
101
140
|
|
102
141
|
describe 'map_key!' do
|
103
142
|
it 'applies function to value under specified key' do
|
104
|
-
transformation =
|
143
|
+
transformation = t(:map_key!, :user, t(:symbolize_keys))
|
105
144
|
|
106
145
|
input = { user: { 'name' => 'Jane' } }
|
107
146
|
output = { user: { name: 'Jane' } }
|
@@ -114,10 +153,10 @@ describe 'Hash mapping with Transproc' do
|
|
114
153
|
|
115
154
|
describe 'nested transform' do
|
116
155
|
it 'applies functions to nested hashes' do
|
117
|
-
symbolize_keys =
|
118
|
-
map_user_key =
|
156
|
+
symbolize_keys = t(:symbolize_keys)
|
157
|
+
map_user_key = t(:map_key, :user, symbolize_keys)
|
119
158
|
|
120
|
-
transformation = symbolize_keys
|
159
|
+
transformation = symbolize_keys >> map_user_key
|
121
160
|
|
122
161
|
input = { 'user' => { 'name' => 'Jane' } }
|
123
162
|
output = { user: { name: 'Jane' } }
|
@@ -128,10 +167,10 @@ describe 'Hash mapping with Transproc' do
|
|
128
167
|
|
129
168
|
describe 'combining transformations' do
|
130
169
|
it 'applies functions to the hash' do
|
131
|
-
symbolize_keys =
|
132
|
-
map =
|
170
|
+
symbolize_keys = t(:symbolize_keys)
|
171
|
+
map = t(:map_hash, user_name: :name, user_email: :email)
|
133
172
|
|
134
|
-
transformation = symbolize_keys
|
173
|
+
transformation = symbolize_keys >> map
|
135
174
|
|
136
175
|
input = { 'user_name' => 'Jade', 'user_email' => 'jade@doe.org' }
|
137
176
|
output = { name: 'Jade', email: 'jade@doe.org' }
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Recursive transformations with Transproc' do
|
4
|
+
describe 'array_recursion' do
|
5
|
+
let(:original) do
|
6
|
+
[
|
7
|
+
'foo',
|
8
|
+
'bar',
|
9
|
+
nil,
|
10
|
+
[
|
11
|
+
'foo',
|
12
|
+
'bar',
|
13
|
+
nil,
|
14
|
+
[
|
15
|
+
'foo',
|
16
|
+
'bar',
|
17
|
+
nil
|
18
|
+
]
|
19
|
+
]
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:input) { original.dup }
|
24
|
+
|
25
|
+
let(:output) do
|
26
|
+
[
|
27
|
+
'foo',
|
28
|
+
'bar',
|
29
|
+
[
|
30
|
+
'foo',
|
31
|
+
'bar',
|
32
|
+
[
|
33
|
+
'foo',
|
34
|
+
'bar'
|
35
|
+
]
|
36
|
+
]
|
37
|
+
]
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when function is non-destructive' do
|
41
|
+
let(:map) { t(:array_recursion, proc { |arr| arr.compact }) }
|
42
|
+
|
43
|
+
it 'applies funtions to all items recursively' do
|
44
|
+
expect(map[input]).to eql(output)
|
45
|
+
expect(input).to eql(original)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'when function is destructive' do
|
50
|
+
let(:map) { t(:array_recursion, proc { |arr| arr.compact! }) }
|
51
|
+
|
52
|
+
it 'applies funtions to all items recursively and destructively' do
|
53
|
+
expect(map[input]).to eql(output)
|
54
|
+
expect(input).to eql(output)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'hash_recursion' do
|
60
|
+
let(:original) do
|
61
|
+
{
|
62
|
+
'foo' => 'bar',
|
63
|
+
'bar' => {
|
64
|
+
'foo' => 'bar',
|
65
|
+
'bar' => {
|
66
|
+
'foo' => 'bar'
|
67
|
+
}
|
68
|
+
},
|
69
|
+
'baz' => 'bar'
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:input) { original.dup }
|
74
|
+
|
75
|
+
let(:output) do
|
76
|
+
{
|
77
|
+
foo: 'bar',
|
78
|
+
bar: {
|
79
|
+
foo: 'bar',
|
80
|
+
bar: {
|
81
|
+
foo: 'bar'
|
82
|
+
}
|
83
|
+
},
|
84
|
+
baz: 'bar'
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when function is non-destructive' do
|
89
|
+
let(:map) { t(:hash_recursion, t(:symbolize_keys)) }
|
90
|
+
|
91
|
+
it 'applies funtions to all values recursively' do
|
92
|
+
expect(map[input]).to eql(output)
|
93
|
+
expect(input).to eql(original)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'when function is destructive' do
|
98
|
+
let(:map) { t(:hash_recursion, t(:symbolize_keys!)) }
|
99
|
+
|
100
|
+
it 'applies funtions to all values recursively and destructively' do
|
101
|
+
expect(map[input]).to eql(output)
|
102
|
+
expect(input).to eql(output)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -6,10 +6,10 @@ describe Transproc do
|
|
6
6
|
input = '1'
|
7
7
|
output = 1.0
|
8
8
|
|
9
|
-
to_i =
|
10
|
-
to_f =
|
9
|
+
to_i = t(-> value { value.to_i })
|
10
|
+
to_f = t(-> value { value.to_f })
|
11
11
|
|
12
|
-
result = to_i
|
12
|
+
result = to_i >> to_f
|
13
13
|
|
14
14
|
expect(result[input]).to eql(output)
|
15
15
|
end
|
@@ -19,7 +19,7 @@ describe Transproc do
|
|
19
19
|
it 'allows registering functions by name' do
|
20
20
|
Transproc.register(:to_boolean, -> value { value == 'true' })
|
21
21
|
|
22
|
-
result =
|
22
|
+
result = t(-> value { value.to_s }) >> t(:to_boolean)
|
23
23
|
|
24
24
|
expect(result[:true]).to be(true)
|
25
25
|
expect(result[:false]).to be(false)
|
@@ -28,7 +28,7 @@ describe Transproc do
|
|
28
28
|
it 'allows registering function by passing a block' do
|
29
29
|
Transproc.register(:to_boolean) { |value| value == 'true' }
|
30
30
|
|
31
|
-
result =
|
31
|
+
result = t(-> value { value.to_s }) >> t(:to_boolean)
|
32
32
|
|
33
33
|
expect(result[:true]).to be(true)
|
34
34
|
expect(result[:false]).to be(false)
|
data/spec/spec_helper.rb
CHANGED
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.1.
|
4
|
+
version: 0.1.3
|
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-
|
11
|
+
date: 2015-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -75,11 +75,13 @@ files:
|
|
75
75
|
- lib/transproc/composer.rb
|
76
76
|
- lib/transproc/function.rb
|
77
77
|
- lib/transproc/hash.rb
|
78
|
+
- lib/transproc/recursion.rb
|
78
79
|
- lib/transproc/version.rb
|
79
80
|
- spec/integration/array_spec.rb
|
80
81
|
- spec/integration/coercions_spec.rb
|
81
82
|
- spec/integration/composer_spec.rb
|
82
83
|
- spec/integration/hash_spec.rb
|
84
|
+
- spec/integration/recursion_spec.rb
|
83
85
|
- spec/integration/transproc_spec.rb
|
84
86
|
- spec/spec_helper.rb
|
85
87
|
- transproc.gemspec
|
@@ -112,5 +114,7 @@ test_files:
|
|
112
114
|
- spec/integration/coercions_spec.rb
|
113
115
|
- spec/integration/composer_spec.rb
|
114
116
|
- spec/integration/hash_spec.rb
|
117
|
+
- spec/integration/recursion_spec.rb
|
115
118
|
- spec/integration/transproc_spec.rb
|
116
119
|
- spec/spec_helper.rb
|
120
|
+
has_rdoc:
|