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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97fa16337a644edef32848dd48ff70278fb40e23bb0ff03fbf800d8dd2bc529e
4
- data.tar.gz: a917d4270f30eb710cc0e93fae3e47ec822aa0e95434a499c8b0886328a94ee3
3
+ metadata.gz: 38765a313ed6b492d7f898519fe1303dcad63ec19e266b2192e94a9f96813a87
4
+ data.tar.gz: 8a1fee97a5ea6a1f9d8e8d296e71e5b5dd1881580683870e9824f40158eb0e97
5
5
  SHA512:
6
- metadata.gz: 3a6d777701ef4c9c42db2a2a103a80ce10e858e92c6c1ca1048498cc8c2c99882cdc4cb1f23eee2384691aef75c01e0fecebd1cde881b84bc7c0816d3f458d31
7
- data.tar.gz: 1a5dd13ac57048a33976d78543eed796e7fcfff31435fcfd299d70051be14cabc9fcbaf81b8bf963b775b0c17fe013956678bab4616ce0312a5db8a2ac17302f
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.if_not do
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.if do
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 Mapper < Remap::Base
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
- # Mapper.call("Hello") # => "Hello!"
96
+ # HelloMapper.call("Hello") # => "Hello!"
97
97
  #
98
98
  # @example Select the second element from an array
99
99
  # class Mapper < Remap::Base
@@ -30,6 +30,11 @@ module Remap
30
30
  def call(state)
31
31
  super.fmap do |input|
32
32
  target.public_send(id, input)
33
+ rescue ArgumentError => e
34
+ raise e.exception("Failed to create [%p] with input [%s] (%s)" % [
35
+ target, input,
36
+ input.class
37
+ ])
33
38
  end
34
39
  end
35
40
  end
@@ -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
- raise ArgumentError,
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
@@ -34,7 +34,9 @@ module Remap
34
34
  return self if path.empty?
35
35
 
36
36
  unless block_given?
37
- raise PathError, trace
37
+ return get(*path, trace: trace) do
38
+ raise PathError, trace
39
+ end
38
40
  end
39
41
 
40
42
  yield
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, [Notice], min_size: 1
8
- attribute? :notices, [Notice], default: EMPTY_ARRAY
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.formatted)
34
+ Error.new(attributes)
35
35
  end
36
36
  end
37
37
  end
@@ -11,7 +11,7 @@ module Remap
11
11
 
12
12
  # @see Iteration#map
13
13
  def call(&block)
14
- state.fatal!("Expected an enumerable, got %s (%s)", value, value.class)
14
+ state.fatal!("Expected an enumerable")
15
15
  end
16
16
  end
17
17
  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
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Remap
4
+ class Notice
5
+ Fatal = Class.new(Error)
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Remap
4
+ class Notice
5
+ Ignore = Class.new(Error)
6
+ end
7
+ 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::Interface
6
+ class Notice < Dry::Concrete
7
7
  attribute? :value, Types::Any
8
8
  attribute :reason, String
9
9
  attribute :path, Array
10
10
 
11
- class Error < Remap::Error
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
- # @return [Error]
30
- def exception
31
- Error.new(self)
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
- fatal(state) do
39
- return ignore(state) do
40
- return notice(state) do
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
- notice = catch :ignore do
42
- return fatal(state) do
43
- return notice(state) do
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
@@ -47,19 +47,19 @@ module Remap
47
47
  raise ArgumentError, "Map#call(state, &error) requires error handler block"
48
48
  end
49
49
 
50
- notice = catch :fatal do
51
- return path.input.call(state) do |inner_state|
52
- other_state = rule.call(inner_state) do |failure|
53
- return error[failure]
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
- callback(other_state) do |failure|
57
- return error[failure]
58
- end
59
- end.then(&path.output)
60
- end
55
+ callback(other_state) do |failure|
56
+ return error[failure]
57
+ end
58
+ end.then(&path.output)
61
59
 
62
- raise notice.traced(backtrace).exception
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.notice!(reason)
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.notice!("#if returned false")
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.notice!("#if_not returned false") : outer_state.value
183
+ bool ? state.ignore!("#if_not returned false") : outer_state.value
184
184
  end
185
185
  end
186
186
  end
@@ -32,7 +32,7 @@ module Remap
32
32
 
33
33
  outer_state.bind(quantifier: "*") do |enum, state|
34
34
  requirement[enum] do
35
- state.fatal!("Expected enumeration but got %p (%s)", enum, enum.class)
35
+ state.fatal!("Expected enumerable")
36
36
  end
37
37
 
38
38
  state.map(&block)
@@ -30,21 +30,18 @@ module Remap
30
30
  # @yieldreturn [State<U>]
31
31
  #
32
32
  # @return [State<U>]
33
- def call(outer_state, &block)
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
- outer_state.bind(index: index) do |array, state|
38
+ state.bind(index: index) do |array, s|
39
39
  requirement[array] do
40
- state.fatal!("Expected array but got %p (%s)", array, array.class)
40
+ s.fatal!("Expected an array")
41
41
  end
42
42
 
43
43
  element = array.fetch(index) do
44
- state.ignore!("Index %s in array %p (%s) not found",
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)
@@ -27,22 +27,18 @@ module Remap
27
27
  # @yieldreturn [State<U>]
28
28
  #
29
29
  # @return [State<U>]
30
- def call(outer_state, &block)
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
- outer_state.bind(key: key) do |hash, state|
35
+ state.bind(key: key) do |hash, s|
36
36
  requirement[hash] do
37
- state.fatal!("Expected hash but got %p (%s)", hash, hash.class)
37
+ s.fatal!("Expected hash")
38
38
  end
39
39
 
40
40
  value = hash.fetch(key) do
41
- state.ignore!("Key %p (%s) not found in hash %p (%s)",
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
- throw :fatal, notice(...)
57
+ notice(...).fatal!
58
58
  end
59
59
 
60
60
  # Throws :warn containing a Notice
61
61
  def notice!(...)
62
- throw :notice, notice(...)
62
+ raise NotImplementedError, "Not implemented"
63
63
  end
64
64
 
65
65
  # Throws :ignore containing a Notice
66
66
  def ignore!(...)
67
- throw :ignore, notice(...)
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) @ %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
- (path + [key]).join("."))
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
- fetch(:value) { return self }.then do |value|
245
- block[value, self] do |reason, **other|
246
- return set(**options, **other).notice!(reason)
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, IndexError => e
268
- error[e.message]
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
- ignore!("Path %s not defined for %p (%s)", e.path.join("."), value, value.class)
275
+ set(path: path + e.path).ignore!("Undefined path")
271
276
  end
272
277
  end
273
278
 
data/lib/remap/types.rb CHANGED
@@ -17,6 +17,7 @@ module Remap
17
17
  Mapper = Interface(:call!)
18
18
  Rule = Interface(:call) | Instance(Proc)
19
19
  Key = Interface(:hash)
20
+ Notice = Instance(Remap::Notice)
20
21
 
21
22
  # Validates a state according to State::Schema
22
23
  State = Hash.constructor do |input, type, &error|
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.30
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/traced.rb
276
- - lib/remap/notice/untraced.rb
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
@@ -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
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Remap
4
- class Notice
5
- class Untraced < Concrete
6
- def traced(backtrace)
7
- Traced.call(**attributes, backtrace: backtrace)
8
- end
9
- end
10
- end
11
- end