remap 2.2.42 → 2.2.46

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: 791eca1356feec6a254c824130b798026851d4f0e56207b32144ba36f64d06a5
4
- data.tar.gz: f7908a2f3dd0d4847860e342d4f11fa298ba5f9689bf8293d585122993ba40cc
3
+ metadata.gz: 71b1a0f8a9cb714d568939ace38b13d053e4c1ddf9131a6040630ff4a1479e4a
4
+ data.tar.gz: fe768a12f868c085839145d28a609a707c4aacb10a18c16e2276e51b23de0fb5
5
5
  SHA512:
6
- metadata.gz: 7eaed51a433437e884ea6af6485ad81752bfb5c7c388e678794c4e17447311b23aaa32d529451fbb70080617b9654f227fa7ac810d3dbff8e7c9c3e403f8de52
7
- data.tar.gz: 3e14bdd50fa6e3ff3ad77589b969b294161a5878c007b9adba387614c3bcd86300e89ae72d9ea0252739269900757747294b7b89b8e0d2b6ad0ae162d363bd47
6
+ metadata.gz: 2f5f657a29f997cf1161eda204a2ddd38f1c6dcc991c8467732d367bc31302c7bafd1e05f5a7655cc5ef46dea5498b9caec6fb9d75d2bd1e9aa5c7f34f7a8ec9
7
+ data.tar.gz: 9263defd99a669a7bcee25381df7a74698cb2ab691b5a2cf22e7cfdb83a8898956b1a17e329e7560e920ad762093822b85c40587dd6bdcf77ab2433188857474
data/lib/remap/base.rb CHANGED
@@ -107,13 +107,12 @@ module Remap
107
107
  include ActiveSupport::Configurable
108
108
  include Dry::Core::Constants
109
109
  include Catchable
110
-
111
110
  extend Mapper::API
112
-
113
111
  using State::Extension
114
112
 
115
113
  with_options instance_accessor: true do |scope|
116
114
  scope.config_accessor(:contract) { Dry::Schema.define {} }
115
+ scope.config_accessor(:config_options) { Config.new }
117
116
  scope.config_accessor(:constructor) { IDENTITY }
118
117
  scope.config_accessor(:options) { EMPTY_ARRAY }
119
118
  scope.config_accessor(:option) { EMPTY_HASH }
@@ -265,6 +264,25 @@ module Remap
265
264
  new(state.options).call(state, &error)
266
265
  end
267
266
 
267
+ # Configuration options for the mapper
268
+ #
269
+ # @yield [Config]
270
+ # @yieldreturn [void]
271
+ #
272
+ # @return [void]
273
+ def self.configuration(&block)
274
+ config = Config.new
275
+ block[config]
276
+ self.config_options = config
277
+ end
278
+
279
+ # @see Mapper::API
280
+ #
281
+ # @private
282
+ def self.validate?
283
+ config_options.validation
284
+ end
285
+
268
286
  # Mappers state according to the mapper rules
269
287
  #
270
288
  # @param state [State]
@@ -287,11 +305,11 @@ module Remap
287
305
  end
288
306
  end
289
307
 
290
- s1 = catch_ignored do |id|
291
- return context.call(state.set(id: id)).then(&constructor).remove_id
308
+ s1 = catch_ignored(state) do |s0|
309
+ return context.call(s0).then(&constructor).remove_id
292
310
  end
293
311
 
294
- Failure.new(failures: s1.notices).then(&error)
312
+ error[s1.failure]
295
313
  end
296
314
 
297
315
  private
@@ -1,21 +1,42 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Remap
4
+ # @api private
4
5
  module Catchable
5
- # @yieldparam id [Symbol]
6
- # @yieldreturn [T]
6
+ using State::Extension
7
+
8
+ # @param state [State]
9
+ #
10
+ # @yieldparam state [State]
11
+ # @yieldparam id [Symbol, String]
12
+ # @yieldreturn [State<T>]
7
13
  #
8
- # @return [T]
9
- def catch_ignored(&block)
10
- catch(to_id(:ignored), &block)
14
+ # @return [State<T>]
15
+ def catch_ignored(state, &block)
16
+ id = to_id(:ignored)
17
+
18
+ catch(id) do
19
+ block[state.set(id: id), id: id].remove_id
20
+ end
11
21
  end
12
22
 
13
- # @yieldparam id [Symbol]
14
- # @yieldreturn [T]
23
+ # @param state [State]
24
+ # @param backtrace [Array<String>]
15
25
  #
16
- # @return [T]
17
- def catch_fatal(&block)
18
- catch(to_id(:fatal), &block)
26
+ # @yieldparam state [State]
27
+ # @yieldparam id [Symbol, String]
28
+ # @yieldreturn [State<T>]
29
+ #
30
+ # @return [State<T>]
31
+ # @raise [Failure::Error]
32
+ def catch_fatal(state, backtrace, &block)
33
+ id = to_id(:fatal)
34
+
35
+ failure = catch(id) do
36
+ return block[state.set(fatal_id: id), fatal_id: id].remove_fatal_id
37
+ end
38
+
39
+ raise failure.exception(backtrace)
19
40
  end
20
41
 
21
42
  private
@@ -548,13 +548,13 @@ module Remap
548
548
  output: [to].flatten,
549
549
  input: path.flatten
550
550
  },
551
+ backtrace: backtrace,
551
552
  rule: call(backtrace: backtrace, &block)
552
553
  })
553
554
  end
554
555
 
555
556
  def build_embed(s0, mapper, backtrace)
556
- f0 = catch_fatal do |fatal_id|
557
- s1 = s0.set(fatal_id: fatal_id)
557
+ catch_fatal(s0, backtrace) do |s1|
558
558
  s2 = s1.set(mapper: mapper)
559
559
  old_mapper = s0.fetch(:mapper)
560
560
 
@@ -563,8 +563,6 @@ module Remap
563
563
  s3.return!
564
564
  end.except(:scope).merge(mapper: old_mapper)
565
565
  end
566
-
567
- raise f0.exception(backtrace)
568
566
  end
569
567
  end
570
568
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Remap
4
+ class Config < OpenStruct
5
+ def initialize
6
+ super(validation: false)
7
+ end
8
+ end
9
+ end
@@ -4,6 +4,8 @@ module Remap
4
4
  module Extensions
5
5
  module Array
6
6
  refine ::Array do
7
+ using Object
8
+
7
9
  # @return [Array<Hash>]
8
10
  def to_hash
9
11
  map(&:to_hash)
@@ -5,7 +5,7 @@ module Remap
5
5
  module Hash
6
6
  refine ::Hash do
7
7
  def formatted
8
- JSON.neat_generate(self, sort: true, wrap: 40, aligned: true, around_colon: 1)
8
+ JSON.neat_generate(compact_blank, sort: true, wrap: 40, aligned: true, around_colon: 1)
9
9
  end
10
10
  end
11
11
  end
@@ -46,6 +46,12 @@ module Remap
46
46
 
47
47
  state1.combine(state2)
48
48
  end
49
+
50
+ # @return [String]
51
+ def inspect
52
+ "%s & %s" % [left, right]
53
+ end
54
+ alias to_s inspect
49
55
  end
50
56
  end
51
57
  end
@@ -8,6 +8,11 @@ module Remap
8
8
 
9
9
  attribute :left, Types::Mapper
10
10
  attribute :right, Types::Mapper
11
+
12
+ # @return [Bool]
13
+ def validate?
14
+ left.validate? && right.validate?
15
+ end
11
16
  end
12
17
  end
13
18
  end
@@ -39,6 +39,12 @@ module Remap
39
39
  end
40
40
  end
41
41
  end
42
+
43
+ # @return [String]
44
+ def inspect
45
+ "%s | %s" % [left, right]
46
+ end
47
+ alias to_s inspect
42
48
  end
43
49
  end
44
50
  end
@@ -5,6 +5,22 @@ module Remap
5
5
  using State::Extension
6
6
 
7
7
  module API
8
+ # @return [Boolean]
9
+ #
10
+ # @abstract
11
+ # @private
12
+ def validate?
13
+ raise NotImplementedError, "`validate?` is not implemented for #{self}"
14
+ end
15
+
16
+ # @param input [Any]
17
+ # @param backtrace [Array<String>]
18
+ # @param options [Hash]
19
+ #
20
+ # @yieldparam [Failure]
21
+ # @yieldreturn [T]
22
+ #
23
+ # @return [Any, T]
8
24
  def call(input, backtrace: caller, **options, &error)
9
25
  unless block_given?
10
26
  return call(input, **options) do |failure|
@@ -44,6 +44,12 @@ module Remap
44
44
 
45
45
  state1.combine(state2).failure("Both left and right passed xor operation").then(&error)
46
46
  end
47
+
48
+ # @return [String]
49
+ def inspect
50
+ "%s ^ %s" % [left, right]
51
+ end
52
+ alias to_s inspect
47
53
  end
48
54
  end
49
55
  end
@@ -11,27 +11,6 @@ module Remap
11
11
  delegate_missing_to :notice
12
12
  delegate :inspect, :to_s, to: :notice
13
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
- e = Traced.new(notice: notice)
29
- e.set_backtrace(backtrace)
30
- e
31
- end
32
- end
33
-
34
- class Traced < Error
35
14
  end
36
15
  end
37
16
  end
data/lib/remap/notice.rb CHANGED
@@ -20,19 +20,5 @@ module Remap
20
20
  def to_hash
21
21
  super.except(:backtrace).compact_blank
22
22
  end
23
-
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)
36
- end
37
23
  end
38
24
  end
@@ -16,29 +16,23 @@ module Remap
16
16
  #
17
17
  # @return [State]
18
18
  def call(state)
19
- s0 = state.except(:value)
19
+ init = state.except(:value)
20
20
 
21
21
  if rules.empty?
22
- return s0
22
+ return init
23
23
  end
24
24
 
25
- failure = catch_fatal do |fatal_id|
26
- s1 = s0.set(fatal_id: fatal_id)
27
- s4 = state.set(fatal_id: fatal_id)
25
+ catch_fatal(init, backtrace) do |s1, fatal_id:|
26
+ s2 = state.set(fatal_id: fatal_id)
28
27
 
29
- return catch_ignored do |id|
30
- s2 = s1.set(id: id)
31
-
32
- rules.reduce(s2) do |s3, rule|
33
- s5 = s3
34
- s6 = rule.call(s4)
35
- s7 = s6.set(id: id)
36
- s5.combine(s7)
28
+ catch_ignored(s1) do |s3, id:|
29
+ rules.reduce(s3) do |s4, rule|
30
+ s5 = rule.call(s2)
31
+ s6 = s5.set(id: id)
32
+ s4.combine(s6)
37
33
  end
38
- end.remove_id.remove_fatal_id
34
+ end
39
35
  end
40
-
41
- raise failure.exception(backtrace)
42
36
  end
43
37
  end
44
38
  end
@@ -5,12 +5,14 @@ module Remap
5
5
  class Map
6
6
  using State::Extension
7
7
 
8
- class Optional < Concrete
8
+ # @api private
9
+ class Optional < Required
9
10
  # Represents an optional mapping rule
10
11
  # When the mapping fails, the value is ignored
11
12
  #
12
13
  # @example Map [:name] to [:nickname]
13
14
  # map = Map::Optional.call({
15
+ # backtrace: caller,
14
16
  # path: {
15
17
  # input: [:name],
16
18
  # output: [:nickname]
@@ -22,16 +24,14 @@ module Remap
22
24
  # })
23
25
  #
24
26
  # output = map.call(state) do |failure|
25
- # raise failure.exeception
27
+ # raise failure.exception(caller)
26
28
  # end
27
29
  #
28
30
  # output.fetch(:value) # => { nickname: "John" }
29
31
  #
30
- # @param state [State]
31
- #
32
- # @return [State]
32
+ # @see Map#call
33
33
  def call(state)
34
- catch { super(state.set(id: _1)).except(:id) }
34
+ catch_ignored(state) { super(_1) }
35
35
  end
36
36
  end
37
37
  end
@@ -8,7 +8,19 @@ module Remap
8
8
  class Required < Concrete
9
9
  attribute :backtrace, Types::Backtrace
10
10
 
11
- # TODO: Remove
11
+ # @see Map#call
12
+ def call(state)
13
+ catch_fatal(state, backtrace) do |s0|
14
+ s2 = path.input.call(s0) do |s1|
15
+ callback(rule.call(s1))
16
+ end
17
+
18
+ s3 = s2.then(&path.output)
19
+ s4 = s3.set(path: state.path)
20
+
21
+ s4.except(:key)
22
+ end
23
+ end
12
24
  end
13
25
  end
14
26
  end
@@ -43,29 +43,14 @@ module Remap
43
43
  #
44
44
  # @abstract
45
45
  def call(state)
46
- failure = catch_fatal do |fatal_id|
47
- s0 = state.set(fatal_id: fatal_id)
48
-
49
- s2 = path.input.call(s0) do |s1|
50
- s2 = rule.call(s1)
51
- callback(s2)
52
- end
53
-
54
- s3 = s2.then(&path.output)
55
- s4 = s3.set(path: state.path)
56
- s5 = s4.except(:key)
57
-
58
- return s5.remove_fatal_id
59
- end
60
-
61
- raise failure.exception(backtrace)
46
+ raise NotImplementedError, "#{self.class}#call not implemented"
62
47
  end
63
48
 
64
49
  # A post-processor method
65
50
  #
66
51
  # @example Up-case mapped value
67
52
  # state = Remap::State.call("Hello World")
68
- # map = Remap::Rule::Map.call({})
53
+ # map = Remap::Rule::Map.call(backtrace: caller)
69
54
  # upcase = map.adjust(&:upcase)
70
55
  # error = -> failure { raise failure.exception }
71
56
  # upcase.call(state, &error).fetch(:value) # => "HELLO WORLD"
@@ -84,7 +69,7 @@ module Remap
84
69
  #
85
70
  # @example Pending mapping
86
71
  # state = Remap::State.call(:value)
87
- # map = Remap::Rule::Map.call({})
72
+ # map = Remap::Rule::Map::Optional.call(backtrace: caller)
88
73
  # pending = map.pending("this is pending")
89
74
  # error = -> failure { raise failure.exception }
90
75
  # pending.call(state, &error).key?(:value) # => false
@@ -99,7 +84,7 @@ module Remap
99
84
  # An enumeration processor
100
85
  #
101
86
  # @example A mapped enum
102
- # enum = Remap::Rule::Map.call({}).enum do
87
+ # enum = Remap::Rule::Map.call(backtrace: caller).enum do
103
88
  # value "A", "B"
104
89
  # otherwise "C"
105
90
  # end
@@ -132,7 +117,7 @@ module Remap
132
117
  # Keeps map, only if block is true
133
118
  #
134
119
  # @example Keep if value contains "A"
135
- # map = Remap::Rule::Map.call({}).if do
120
+ # map = Remap::Rule::Map::Optional.call(backtrace: caller).if do
136
121
  # value.include?("A")
137
122
  # end
138
123
  #
@@ -160,11 +145,11 @@ module Remap
160
145
  #
161
146
 
162
147
  # @example Keep unless value contains "A"
163
- # map = Remap::Rule::Map.call({}).if_not do
148
+ # map = Remap::Rule::Map::Optional.new(backtrace: caller).if_not do
164
149
  # value.include?("A")
165
150
  # end
166
151
  #
167
- # error = -> failure { raise failure.exception }
152
+ # error = -> failure { raise failure.exception(caller) }
168
153
  #
169
154
  # a = Remap::State.call("A")
170
155
  # map.call(a, &error).key?(:value) # => false
@@ -53,7 +53,7 @@ module Remap
53
53
  end
54
54
  end
55
55
 
56
- # Throws :fatal containing a Notice
56
+ # @see #notice
57
57
  def fatal!(...)
58
58
  fatal_id = fetch(:fatal_id) do
59
59
  raise ArgumentError, "Missing :fatal_id in %s" % formatted
@@ -62,12 +62,7 @@ module Remap
62
62
  throw fatal_id, Failure.new(failures: [notice(...)], notices: notices)
63
63
  end
64
64
 
65
- # Throws :warn containing a Notice
66
- def notice!(...)
67
- raise NotImplementedError, "Not implemented"
68
- end
69
-
70
- # Throws :ignore containing a Notice
65
+ # @see #notice
71
66
  def ignore!(...)
72
67
  set(notice: notice(...)).except(:value).return!
73
68
  end
@@ -92,6 +87,8 @@ module Remap
92
87
  #
93
88
  # @return [self]
94
89
  def _(&block)
90
+ return self unless mapper.validate?
91
+
95
92
  unless block
96
93
  return _ do |reason|
97
94
  raise ArgumentError, "[BUG] State: #{formatted} reason: #{reason.formatted}"
@@ -320,6 +317,11 @@ module Remap
320
317
  fetch(:id)
321
318
  end
322
319
 
320
+ # @return [Mapper::API]
321
+ def mapper
322
+ fetch(:mapper)
323
+ end
324
+
323
325
  # @return [Array<Symbol>]
324
326
  def ids
325
327
  fetch(:ids)
@@ -378,6 +380,8 @@ module Remap
378
380
  # @return [Failure]
379
381
  def failure(reason = Undefined)
380
382
  failures = case [path, reason]
383
+ in [_, Undefined]
384
+ return Failure.new(failures: notices)
381
385
  in [_, Notice => notice]
382
386
  [notice]
383
387
  in [path, Array => reasons]
@@ -9,8 +9,12 @@ module Remap
9
9
  required(:notices).array(Types.Instance(Notice))
10
10
  required(:options).value(:hash)
11
11
  required(:path).array(Types::Key)
12
- required(:ids).value(:array)
13
- required(:fatal_ids).value(:array)
12
+
13
+ required(:ids).array(Types::ID)
14
+ optional(:id).filled(Types::ID)
15
+
16
+ required(:fatal_ids).array(Types::ID)
17
+ optional(:fatal_id).filled(Types::ID)
14
18
 
15
19
  optional(:index).filled(:integer)
16
20
  optional(:element).filled
data/lib/remap/types.rb CHANGED
@@ -18,6 +18,7 @@ module Remap
18
18
  Rule = Interface(:call) | Instance(Proc)
19
19
  Key = Interface(:hash)
20
20
  Notice = Instance(Remap::Notice)
21
+ ID = String | Symbol
21
22
 
22
23
  # Validates a state according to State::Schema
23
24
  State = Hash.constructor do |input, type, &error|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remap
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.42
4
+ version: 2.2.46
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linus Oleander
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-15 00:00:00.000000000 Z
11
+ date: 2021-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -251,6 +251,7 @@ files:
251
251
  - lib/remap/catchable.rb
252
252
  - lib/remap/class_interface.rb
253
253
  - lib/remap/compiler.rb
254
+ - lib/remap/config.rb
254
255
  - lib/remap/constructor.rb
255
256
  - lib/remap/constructor/argument.rb
256
257
  - lib/remap/constructor/keyword.rb