remap 2.2.41 → 2.2.45

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: 5a8ee7751985c548b272cb12bb0b08b655f52de3143075afafd8578f9e604eba
4
- data.tar.gz: f333d2922e7d5ef7a6bd050eb2fc0261446097fdd17f2745eef1a3bc35eb96ee
3
+ metadata.gz: dd36dff737c5c3965b1a7bc5dd643bc83098a908c7dac7426a13e6c40a76af95
4
+ data.tar.gz: 958e0d19bc7d57a72697af4c1dea86f47810f87025a99ab1e13824f82822ce53
5
5
  SHA512:
6
- metadata.gz: 69b98a323c17562a06d4b83f3455c2b1c7e6a482280f5afe283f0d9570411391bb2d0de353d96250436d8aa51ad8443f1a3e25805dde0d6baebbc5286d019a9e
7
- data.tar.gz: 86f03041d4cf3ef2ebd1c597269645ae83c55e5d3326077a9d55f51f3f0a47aea131523e9e314d635f34d60487dbba20350a28ffa3eca9b032f0aecd01bf006f
6
+ metadata.gz: 62e4ad82b773e57a113f190ab5cb3d238e9e0059aa4f692475c8372c91ed25dc9c564782a0522cb1902c2d5f408f0109136487ceb6176bfd5e0e4dd3b92cba23
7
+ data.tar.gz: 647d4ee1c543073752b92d4e04a7deadde0dd4522ae793cf475544f58a88986d9077bf44fcb2b3cf0acf2b9ee7d5400ff4c95d246f05b78f9e9ce62c800e0c2f
data/lib/remap/base.rb CHANGED
@@ -106,13 +106,13 @@ module Remap
106
106
  class Base < Mapper
107
107
  include ActiveSupport::Configurable
108
108
  include Dry::Core::Constants
109
-
110
109
  include Catchable
111
-
110
+ extend Mapper::API
112
111
  using State::Extension
113
112
 
114
113
  with_options instance_accessor: true do |scope|
115
114
  scope.config_accessor(:contract) { Dry::Schema.define {} }
115
+ scope.config_accessor(:config_options) { Config.new }
116
116
  scope.config_accessor(:constructor) { IDENTITY }
117
117
  scope.config_accessor(:options) { EMPTY_ARRAY }
118
118
  scope.config_accessor(:option) { EMPTY_HASH }
@@ -122,31 +122,6 @@ module Remap
122
122
 
123
123
  schema schema.strict(false)
124
124
 
125
- # extend Operation
126
-
127
- def self.call(input, backtrace: caller, **options, &error)
128
- unless block_given?
129
- return call(input, **options) do |failure|
130
- raise failure.exception(backtrace)
131
- end
132
- end
133
-
134
- s0 = State.call(input, options: options, mapper: self)._
135
-
136
- s1 = call!(s0) do |failure|
137
- return error[failure]
138
- end
139
-
140
- case s1
141
- in { value: value }
142
- value
143
- in { notices: [] }
144
- error[s1.failure("No data could be mapped")]
145
- in { notices: }
146
- error[Failure.new(failures: notices)]
147
- end
148
- end
149
-
150
125
  # Defines a schema for the mapper
151
126
  # If the schema fail, the mapper will fail
152
127
  #
@@ -289,6 +264,25 @@ module Remap
289
264
  new(state.options).call(state, &error)
290
265
  end
291
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
+
292
286
  # Mappers state according to the mapper rules
293
287
  #
294
288
  # @param state [State]
@@ -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
@@ -4,10 +4,15 @@ module Remap
4
4
  class Mapper
5
5
  # @abstract
6
6
  class Binary < self
7
- include Operation
7
+ include API
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
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Remap
4
+ class Mapper
5
+ using State::Extension
6
+
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]
24
+ def call(input, backtrace: caller, **options, &error)
25
+ unless block_given?
26
+ return call(input, **options) do |failure|
27
+ raise failure.exception(backtrace)
28
+ end
29
+ end
30
+
31
+ s0 = State.call(input, options: options, mapper: self)._
32
+
33
+ s1 = call!(s0) do |failure|
34
+ return error[failure]
35
+ end
36
+
37
+ case s1
38
+ in { value: value }
39
+ value
40
+ in { notices: [] }
41
+ error[s1.failure("No data could be mapped")]
42
+ in { notices: }
43
+ error[Failure.new(failures: notices)]
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -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
@@ -31,7 +31,7 @@ module Remap
31
31
  #
32
32
  # @return [State]
33
33
  def call(state)
34
- catch { super(state.set(id: _1)).except(:id) }
34
+ catch_ignored { super(state.set(id: _1)).remove_id }
35
35
  end
36
36
  end
37
37
  end
@@ -92,6 +92,8 @@ module Remap
92
92
  #
93
93
  # @return [self]
94
94
  def _(&block)
95
+ return self unless mapper.validate?
96
+
95
97
  unless block
96
98
  return _ do |reason|
97
99
  raise ArgumentError, "[BUG] State: #{formatted} reason: #{reason.formatted}"
@@ -320,6 +322,11 @@ module Remap
320
322
  fetch(:id)
321
323
  end
322
324
 
325
+ # @return [Mapper::API]
326
+ def mapper
327
+ fetch(:mapper)
328
+ end
329
+
323
330
  # @return [Array<Symbol>]
324
331
  def ids
325
332
  fetch(:ids)
@@ -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|
data/lib/remap.rb CHANGED
@@ -19,6 +19,7 @@ module Remap
19
19
  loader = Zeitwerk::Loader.for_gem
20
20
  loader.collapse("#{__dir__}/remap/mapper/support")
21
21
  loader.setup
22
+ loader.inflector.inflect "api" => "API"
22
23
  loader.eager_load
23
24
 
24
25
  include ClassInterface
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.41
4
+ version: 2.2.45
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
@@ -271,6 +272,7 @@ files:
271
272
  - lib/remap/mapper/and.rb
272
273
  - lib/remap/mapper/binary.rb
273
274
  - lib/remap/mapper/or.rb
275
+ - lib/remap/mapper/support/api.rb
274
276
  - lib/remap/mapper/support/operations.rb
275
277
  - lib/remap/mapper/xor.rb
276
278
  - lib/remap/nothing.rb
@@ -278,7 +280,6 @@ files:
278
280
  - lib/remap/notice/error.rb
279
281
  - lib/remap/notice/fatal.rb
280
282
  - lib/remap/notice/ignore.rb
281
- - lib/remap/operation.rb
282
283
  - lib/remap/path.rb
283
284
  - lib/remap/path/input.rb
284
285
  - lib/remap/path/output.rb
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Remap
4
- using State::Extension
5
-
6
- # Class interface for {Remap::Base} and instance interface for {Mapper}
7
- module Operation
8
- # Public interface for mappers
9
- #
10
- # @param input [Any] data to be mapped
11
- # @param options [Hash] mapper options
12
- #
13
- # @yield [Failure]
14
- # when a non-critical error occurs
15
- # @yieldreturn T
16
- #
17
- # @return [Any, T]
18
- # when request is a success
19
- # @raise [Remap::Error]
20
- # when a fatal error occurs
21
- def call(input, backtrace: caller, **options, &error)
22
- unless block_given?
23
- return call(input, **options) do |failure|
24
- raise failure.exception(backtrace)
25
- end
26
- end
27
-
28
- s0 = State.call(input, options: options, mapper: self)._
29
-
30
- s1 = call!(s0) do |failure|
31
- return error[failure]
32
- end
33
-
34
- case s1
35
- in { value: value }
36
- value
37
- in { notices: [] }
38
- error[s1.failure("No data could be mapped")]
39
- in { notices: }
40
- error[Failure.new(failures: notices)]
41
- end
42
- end
43
- end
44
- end