remap 2.2.30 → 2.2.35
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/remap/base.rb +4 -4
- data/lib/remap/constructor/argument.rb +5 -0
- data/lib/remap/constructor/keyword.rb +9 -3
- data/lib/remap/extensions/object.rb +3 -1
- data/lib/remap/failure.rb +3 -3
- data/lib/remap/iteration/other.rb +1 -1
- data/lib/remap/notice/error.rb +32 -0
- data/lib/remap/notice/fatal.rb +7 -0
- data/lib/remap/notice/ignore.rb +7 -0
- data/lib/remap/notice.rb +14 -10
- data/lib/remap/rule/map/optional.rb +3 -7
- data/lib/remap/rule/map/required.rb +3 -9
- data/lib/remap/rule/map.rb +14 -14
- data/lib/remap/selector/all.rb +1 -1
- data/lib/remap/selector/index.rb +4 -7
- data/lib/remap/selector/key.rb +4 -8
- data/lib/remap/state/extension.rb +21 -16
- data/lib/remap/types.rb +1 -0
- metadata +4 -3
- data/lib/remap/notice/traced.rb +0 -19
- data/lib/remap/notice/untraced.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38765a313ed6b492d7f898519fe1303dcad63ec19e266b2192e94a9f96813a87
|
4
|
+
data.tar.gz: 8a1fee97a5ea6a1f9d8e8d296e71e5b5dd1881580683870e9824f40158eb0e97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3542fc58722ff987b0307c83e339659af0bfcd358dabfbbec338c582f66cb78d10c0e325ca4ba319bb925001c14cc8f6174e189a7435abece4aff0e9276b716c
|
7
|
+
data.tar.gz: 1b82a610fd18d8583ff6f26098e733cbea67a92381af7bcd20cc0a15e41fc025e3a8642678fdd40a3e8224ecbd2abd5d0b6c29f19163302224bf670e452d000b
|
data/lib/remap/base.rb
CHANGED
@@ -37,7 +37,7 @@ module Remap
|
|
37
37
|
# class Mapper < Remap::Base
|
38
38
|
# define do
|
39
39
|
# each do
|
40
|
-
# map
|
40
|
+
# map?.if_not do
|
41
41
|
# value.include?("B")
|
42
42
|
# end
|
43
43
|
# end
|
@@ -50,7 +50,7 @@ module Remap
|
|
50
50
|
# class Mapper < Remap::Base
|
51
51
|
# define do
|
52
52
|
# each do
|
53
|
-
# map
|
53
|
+
# map?.if do
|
54
54
|
# value.include?("B")
|
55
55
|
# end
|
56
56
|
# end
|
@@ -85,7 +85,7 @@ module Remap
|
|
85
85
|
# Mapper.call({ people: [{ name: "John" }] }) # => { names: ["John"] }
|
86
86
|
#
|
87
87
|
# @example Map "Hello" to "Hello!"
|
88
|
-
# class
|
88
|
+
# class HelloMapper < Remap::Base
|
89
89
|
# define do
|
90
90
|
# map.adjust do
|
91
91
|
# "#{value}!"
|
@@ -93,7 +93,7 @@ module Remap
|
|
93
93
|
# end
|
94
94
|
# end
|
95
95
|
#
|
96
|
-
#
|
96
|
+
# HelloMapper.call("Hello") # => "Hello!"
|
97
97
|
#
|
98
98
|
# @example Select the second element from an array
|
99
99
|
# class Mapper < Remap::Base
|
@@ -27,13 +27,19 @@ module Remap
|
|
27
27
|
#
|
28
28
|
# @return [State]
|
29
29
|
def call(state)
|
30
|
-
super.fmap do |input|
|
30
|
+
super.fmap do |input, &error|
|
31
31
|
unless input.is_a?(Hash)
|
32
|
-
|
33
|
-
"Keyword stategy requires a Hash, got %s (%s)" % [input, input.class]
|
32
|
+
return error["Input is not a hash"]
|
34
33
|
end
|
35
34
|
|
36
35
|
target.public_send(id, **input)
|
36
|
+
rescue ArgumentError => e
|
37
|
+
raise e.exception("Failed to create [%p] with input [%s] (%s}) using method %s" % [
|
38
|
+
target,
|
39
|
+
input,
|
40
|
+
input.class,
|
41
|
+
id
|
42
|
+
])
|
37
43
|
end
|
38
44
|
end
|
39
45
|
end
|
data/lib/remap/failure.rb
CHANGED
@@ -4,8 +4,8 @@ module Remap
|
|
4
4
|
using Extensions::Hash
|
5
5
|
|
6
6
|
class Failure < Dry::Concrete
|
7
|
-
attribute :failures,
|
8
|
-
attribute? :notices,
|
7
|
+
attribute :failures, Types.Array(Types::Notice), min_size: 1
|
8
|
+
attribute? :notices, Types.Array(Types::Notice), default: EMPTY_ARRAY
|
9
9
|
|
10
10
|
# Merges two failures and returns a new failure
|
11
11
|
#
|
@@ -31,7 +31,7 @@ module Remap
|
|
31
31
|
|
32
32
|
# @return [Error]
|
33
33
|
def exception
|
34
|
-
Error.new(attributes
|
34
|
+
Error.new(attributes)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Remap
|
4
|
+
class Notice
|
5
|
+
using Extensions::Hash
|
6
|
+
using State::Extension
|
7
|
+
|
8
|
+
class Error < Remap::Error
|
9
|
+
extend Dry::Initializer
|
10
|
+
|
11
|
+
delegate_missing_to :notice
|
12
|
+
delegate :inspect, :to_s, to: :notice
|
13
|
+
option :notice, type: Types.Instance(Notice)
|
14
|
+
|
15
|
+
def inspect
|
16
|
+
"#<%s %s>" % [self.class, to_hash.formatted]
|
17
|
+
end
|
18
|
+
|
19
|
+
def undefined(state)
|
20
|
+
state.set(notice: notice).except(:value)
|
21
|
+
end
|
22
|
+
|
23
|
+
def failure(state)
|
24
|
+
Failure.new(failures: [notice], notices: state.fetch(:notices))
|
25
|
+
end
|
26
|
+
|
27
|
+
def traced(backtrace)
|
28
|
+
tap { _1.set_backtrace(backtrace) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/remap/notice.rb
CHANGED
@@ -3,17 +3,12 @@
|
|
3
3
|
module Remap
|
4
4
|
using Extensions::Hash
|
5
5
|
|
6
|
-
class Notice < Dry::
|
6
|
+
class Notice < Dry::Concrete
|
7
7
|
attribute? :value, Types::Any
|
8
8
|
attribute :reason, String
|
9
9
|
attribute :path, Array
|
10
10
|
|
11
|
-
|
12
|
-
extend Dry::Initializer
|
13
|
-
|
14
|
-
param :notice, type: Notice
|
15
|
-
end
|
16
|
-
|
11
|
+
# @return [String]
|
17
12
|
def inspect
|
18
13
|
"#<%s %s>" % [self.class, to_hash.formatted]
|
19
14
|
end
|
@@ -26,9 +21,18 @@ module Remap
|
|
26
21
|
super.except(:backtrace).compact_blank
|
27
22
|
end
|
28
23
|
|
29
|
-
#
|
30
|
-
|
31
|
-
|
24
|
+
# Used by State to skip mapping rules
|
25
|
+
#
|
26
|
+
# @raise [Notice::Ignore]
|
27
|
+
def ignore!
|
28
|
+
raise Ignore.new(notice: self)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Used by the state to halt mappers
|
32
|
+
#
|
33
|
+
# @raise [Notice::Fatal]
|
34
|
+
def fatal!
|
35
|
+
raise Fatal.new(notice: self)
|
32
36
|
end
|
33
37
|
end
|
34
38
|
end
|
@@ -35,13 +35,9 @@ module Remap
|
|
35
35
|
raise ArgumentError, "map.call(state, &error) requires a block"
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
return super
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
38
|
+
super
|
39
|
+
rescue Notice::Ignore => e
|
40
|
+
e.undefined(state)
|
45
41
|
end
|
46
42
|
|
47
43
|
private
|
@@ -38,15 +38,9 @@ module Remap
|
|
38
38
|
raise ArgumentError, "Required.call(state, &error) requires a block"
|
39
39
|
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
return super
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
error[state.failure(notice)]
|
41
|
+
super
|
42
|
+
rescue Notice::Ignore => e
|
43
|
+
error[e.failure(state)]
|
50
44
|
end
|
51
45
|
end
|
52
46
|
end
|
data/lib/remap/rule/map.rb
CHANGED
@@ -47,19 +47,19 @@ module Remap
|
|
47
47
|
raise ArgumentError, "Map#call(state, &error) requires error handler block"
|
48
48
|
end
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
50
|
+
s1 = path.input.call(state) do |inner_state|
|
51
|
+
other_state = rule.call(inner_state) do |failure|
|
52
|
+
return error[failure]
|
53
|
+
end
|
55
54
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
55
|
+
callback(other_state) do |failure|
|
56
|
+
return error[failure]
|
57
|
+
end
|
58
|
+
end.then(&path.output)
|
61
59
|
|
62
|
-
|
60
|
+
s1.set(path: state.path).except(:key)
|
61
|
+
rescue Notice::Fatal => e
|
62
|
+
raise e.traced(backtrace)
|
63
63
|
end
|
64
64
|
|
65
65
|
# A post-processor method
|
@@ -93,7 +93,7 @@ module Remap
|
|
93
93
|
# @return [Map]
|
94
94
|
def pending(reason = "Pending mapping")
|
95
95
|
add do |state|
|
96
|
-
state.
|
96
|
+
state.ignore!(reason)
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -152,7 +152,7 @@ module Remap
|
|
152
152
|
def if(&block)
|
153
153
|
add do |outer_state|
|
154
154
|
outer_state.execute(&block).fmap do |bool, state|
|
155
|
-
bool ? outer_state.value : state.
|
155
|
+
bool ? outer_state.value : state.ignore!("#if returned false")
|
156
156
|
end
|
157
157
|
end
|
158
158
|
end
|
@@ -180,7 +180,7 @@ module Remap
|
|
180
180
|
def if_not(&block)
|
181
181
|
add do |outer_state|
|
182
182
|
outer_state.execute(&block).fmap do |bool, state|
|
183
|
-
bool ? state.
|
183
|
+
bool ? state.ignore!("#if_not returned false") : outer_state.value
|
184
184
|
end
|
185
185
|
end
|
186
186
|
end
|
data/lib/remap/selector/all.rb
CHANGED
data/lib/remap/selector/index.rb
CHANGED
@@ -30,21 +30,18 @@ module Remap
|
|
30
30
|
# @yieldreturn [State<U>]
|
31
31
|
#
|
32
32
|
# @return [State<U>]
|
33
|
-
def call(
|
33
|
+
def call(state, &block)
|
34
34
|
unless block_given?
|
35
35
|
raise ArgumentError, "The index selector requires an iteration block"
|
36
36
|
end
|
37
37
|
|
38
|
-
|
38
|
+
state.bind(index: index) do |array, s|
|
39
39
|
requirement[array] do
|
40
|
-
|
40
|
+
s.fatal!("Expected an array")
|
41
41
|
end
|
42
42
|
|
43
43
|
element = array.fetch(index) do
|
44
|
-
|
45
|
-
index,
|
46
|
-
array,
|
47
|
-
array.class)
|
44
|
+
s.ignore!("Index %s not found", index)
|
48
45
|
end
|
49
46
|
|
50
47
|
state.set(element, index: index).then(&block)
|
data/lib/remap/selector/key.rb
CHANGED
@@ -27,22 +27,18 @@ module Remap
|
|
27
27
|
# @yieldreturn [State<U>]
|
28
28
|
#
|
29
29
|
# @return [State<U>]
|
30
|
-
def call(
|
30
|
+
def call(state, &block)
|
31
31
|
unless block_given?
|
32
32
|
raise ArgumentError, "The key selector requires an iteration block"
|
33
33
|
end
|
34
34
|
|
35
|
-
|
35
|
+
state.bind(key: key) do |hash, s|
|
36
36
|
requirement[hash] do
|
37
|
-
|
37
|
+
s.fatal!("Expected hash")
|
38
38
|
end
|
39
39
|
|
40
40
|
value = hash.fetch(key) do
|
41
|
-
|
42
|
-
key,
|
43
|
-
key.class,
|
44
|
-
hash,
|
45
|
-
hash.class)
|
41
|
+
s.ignore!("Key not found")
|
46
42
|
end
|
47
43
|
|
48
44
|
state.set(value, key: key).then(&block)
|
@@ -54,17 +54,17 @@ module Remap
|
|
54
54
|
|
55
55
|
# Throws :fatal containing a Notice
|
56
56
|
def fatal!(...)
|
57
|
-
|
57
|
+
notice(...).fatal!
|
58
58
|
end
|
59
59
|
|
60
60
|
# Throws :warn containing a Notice
|
61
61
|
def notice!(...)
|
62
|
-
|
62
|
+
raise NotImplementedError, "Not implemented"
|
63
63
|
end
|
64
64
|
|
65
65
|
# Throws :ignore containing a Notice
|
66
66
|
def ignore!(...)
|
67
|
-
|
67
|
+
notice(...).ignore!
|
68
68
|
end
|
69
69
|
|
70
70
|
# Creates a notice containing the given message
|
@@ -132,12 +132,12 @@ module Remap
|
|
132
132
|
list1 + list2
|
133
133
|
in [:value, left, right]
|
134
134
|
fatal!(
|
135
|
-
"Could not merge [%p] (%s) with [%p] (%s)
|
136
|
-
left,
|
135
|
+
"Could not merge [%p] (%s) with [%p] (%s)",
|
136
|
+
left.formatted,
|
137
137
|
left.class,
|
138
|
-
right,
|
139
|
-
right.class
|
140
|
-
|
138
|
+
right.formatted,
|
139
|
+
right.class
|
140
|
+
)
|
141
141
|
in [:notices, Array => n1, Array => n2]
|
142
142
|
n1 + n2
|
143
143
|
in [Symbol, _, value]
|
@@ -218,7 +218,8 @@ module Remap
|
|
218
218
|
Notice.call(
|
219
219
|
reason: inner_reason,
|
220
220
|
path: path + sufix,
|
221
|
-
**only(:value)
|
221
|
+
**only(:value)
|
222
|
+
)
|
222
223
|
end
|
223
224
|
end
|
224
225
|
end
|
@@ -241,9 +242,11 @@ module Remap
|
|
241
242
|
raise ArgumentError, "State#bind requires a block"
|
242
243
|
end
|
243
244
|
|
244
|
-
|
245
|
-
|
246
|
-
|
245
|
+
s = set(**options)
|
246
|
+
|
247
|
+
fetch(:value) { return s }.then do |value|
|
248
|
+
block[value, s] do |reason, **other|
|
249
|
+
return s.set(**other).ignore!(reason)
|
247
250
|
end
|
248
251
|
end
|
249
252
|
end
|
@@ -263,11 +266,13 @@ module Remap
|
|
263
266
|
return error["Undefined returned, skipping!"]
|
264
267
|
end
|
265
268
|
|
266
|
-
set(result)
|
267
|
-
rescue KeyError
|
268
|
-
|
269
|
+
set(result)
|
270
|
+
rescue KeyError => e
|
271
|
+
set(path: path + [e.key]).ignore!(e.message)
|
272
|
+
rescue IndexError => e
|
273
|
+
ignore!(e.message)
|
269
274
|
rescue PathError => e
|
270
|
-
|
275
|
+
set(path: path + e.path).ignore!("Undefined path")
|
271
276
|
end
|
272
277
|
end
|
273
278
|
|
data/lib/remap/types.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.35
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Linus Oleander
|
@@ -272,8 +272,9 @@ files:
|
|
272
272
|
- lib/remap/mapper/xor.rb
|
273
273
|
- lib/remap/nothing.rb
|
274
274
|
- lib/remap/notice.rb
|
275
|
-
- lib/remap/notice/
|
276
|
-
- lib/remap/notice/
|
275
|
+
- lib/remap/notice/error.rb
|
276
|
+
- lib/remap/notice/fatal.rb
|
277
|
+
- lib/remap/notice/ignore.rb
|
277
278
|
- lib/remap/operation.rb
|
278
279
|
- lib/remap/path.rb
|
279
280
|
- lib/remap/path/input.rb
|
data/lib/remap/notice/traced.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Remap
|
4
|
-
class Notice
|
5
|
-
class Traced < Concrete
|
6
|
-
attribute? :backtrace, Types::Backtrace, default: EMPTY_ARRAY
|
7
|
-
|
8
|
-
def traced(backtrace)
|
9
|
-
Notice.call(**attributes, backtrace: backtrace)
|
10
|
-
end
|
11
|
-
|
12
|
-
def exception
|
13
|
-
return super if backtrace.blank?
|
14
|
-
|
15
|
-
super.tap { _1.set_backtrace(backtrace.map(&:to_s)) }
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|