remap 2.1.14 → 2.1.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88204af4f42ce85893ec029d953e2e6c417bbacba673522eedadca9561f8f749
4
- data.tar.gz: 9e3e6040baf0426590b4c920d33f0979e74a084a19bbd87079d474a9e251663a
3
+ metadata.gz: e3439c23b054b2470688ec55cedb4dc0d7c0f77544c45f8f47c82b5d8d9b954a
4
+ data.tar.gz: 515558fee642db02bd30e5194846a4486f6b59c67830c46581b9b3d3c529532e
5
5
  SHA512:
6
- metadata.gz: 612cf547b8207f305cf9b1f4788d3b36c0d487f0c42bdbf657336c04982060f6dd46a0cfc2a57c39588cef15287ede98205ab5beb46c9245b3270a99b586934d
7
- data.tar.gz: a11492555b465f2d9f2db0611ed8d6457579dc6546805a9a867fc7b951061572f0faae84caec4be50907efa99e15647a8ae52aefd0de14362d29d6fea45cb166
6
+ metadata.gz: 599b8f2de0d62e5d4ab8ded2d303243fe11292f0cc621b7cdf66bb9e70ac712bb3fe4811cd926e9929a06f110445e283b2b8c3134a68cec5c810c53f572f720c
7
+ data.tar.gz: 6cf4704750445ac8dfdac08cfe2beec2bff5ac34db4c61557f9239b49857c3a1423978d6cc38342917753d48b4edeeff397d012c8b91d32a3ee49ec96ab62fa4
data/lib/remap/base.rb CHANGED
@@ -143,7 +143,7 @@ module Remap
143
143
  #
144
144
  # @return [void]
145
145
  def self.contract(&context)
146
- self.contract = Dry::Schema.JSON(&context)
146
+ self.contract = Dry::Schema.define(&context)
147
147
  end
148
148
 
149
149
  # Defines a rule for the mapper
@@ -236,14 +236,12 @@ module Remap
236
236
  #
237
237
  # @return [void]
238
238
  def self.define(target = Nothing, method: :new, strategy: :argument, &context)
239
- unless context
240
- raise ArgumentError, "Missing block"
239
+ unless block_given?
240
+ raise ArgumentError, "#{self}.define requires a block"
241
241
  end
242
242
 
243
- self.context = Compiler.call(&context)
244
243
  self.constructor = Constructor.call(method: method, strategy: strategy, target: target)
245
- rescue Dry::Struct::Error => e
246
- raise ArgumentError, e.message
244
+ self.context = Compiler.call(&context)
247
245
  end
248
246
 
249
247
  # Similar to {::call}, but takes a state
@@ -269,7 +267,7 @@ module Remap
269
267
  #
270
268
  # @private
271
269
  def call(state, &error)
272
- unless error
270
+ unless block_given?
273
271
  raise ArgumentError, "Base#call(state, &error) requires block"
274
272
  end
275
273
 
@@ -30,7 +30,7 @@ module Remap
30
30
  #
31
31
  # @return [Rule]
32
32
  def self.call(&block)
33
- unless block
33
+ unless block_given?
34
34
  return Rule::VOID
35
35
  end
36
36
 
@@ -38,10 +38,6 @@ module Remap
38
38
  compiler.instance_exec(&block)
39
39
  end.rules
40
40
 
41
- if rules.empty?
42
- return Rule::VOID
43
- end
44
-
45
41
  Rule::Block.new(rules)
46
42
  end
47
43
 
@@ -66,7 +62,7 @@ module Remap
66
62
  # output.fetch(:value) # => { nickname: "John" }
67
63
  #
68
64
  # @return [Rule::Map::Required]
69
- def map(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block)
65
+ def map(*path, to: EMPTY_ARRAY, backtrace: caller, &block)
70
66
  add rule(*path, to: to, backtrace: backtrace, &block)
71
67
  end
72
68
 
@@ -93,7 +89,7 @@ module Remap
93
89
  # @see #map
94
90
  #
95
91
  # @return [Rule::Map::Optional]
96
- def map?(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block)
92
+ def map?(*path, to: EMPTY_ARRAY, backtrace: caller, &block)
97
93
  add rule?(*path, to: to, backtrace: backtrace, &block)
98
94
  end
99
95
 
@@ -117,7 +113,7 @@ module Remap
117
113
  # output.fetch(:value) # => { name: "John" }
118
114
  #
119
115
  # @return [Rule::Map::Required]
120
- def get(*path, backtrace: Kernel.caller, &block)
116
+ def get(*path, backtrace: caller, &block)
121
117
  add rule(path, to: path, backtrace: backtrace, &block)
122
118
  end
123
119
 
@@ -142,7 +138,7 @@ module Remap
142
138
  # @see #get
143
139
  #
144
140
  # @return [Rule::Map::Optional]
145
- def get?(*path, backtrace: Kernel.caller, &block)
141
+ def get?(*path, backtrace: caller, &block)
146
142
  add rule?(path, to: path, backtrace: backtrace, &block)
147
143
  end
148
144
 
@@ -180,12 +176,12 @@ module Remap
180
176
  # output.fetch(:value) # => { car: "Volvo" }
181
177
  #
182
178
  # @return [Rule::Embed]
183
- def embed(mapper, &block)
184
- if block
179
+ def embed(mapper)
180
+ if block_given?
185
181
  raise ArgumentError, "#embed does not take a block"
186
182
  end
187
183
 
188
- embeding = rule(&block).add do |state, &error|
184
+ embeding = rule.add do |state, &error|
189
185
  mapper.call!(state.set(mapper: mapper)) do |failure|
190
186
  next error[failure]
191
187
  end.except(:mapper, :scope)
@@ -229,8 +225,8 @@ module Remap
229
225
  # @raise [ArgumentError]
230
226
  # if no path given
231
227
  # if path is not a Symbol or Array<Symbol>
232
- def set(*path, to:, &block)
233
- if block
228
+ def set(*path, to:)
229
+ if block_given?
234
230
  raise ArgumentError, "#set does not take a block"
235
231
  end
236
232
 
@@ -258,7 +254,7 @@ module Remap
258
254
  # output.fetch(:value) # => { nickname: "John" }
259
255
  #
260
256
  # @return [Rule::Map]
261
- def to(*path, map: EMPTY_ARRAY, backtrace: Kernel.caller, &block)
257
+ def to(*path, map: EMPTY_ARRAY, backtrace: caller, &block)
262
258
  add rule(*map, to: path, backtrace: backtrace, &block)
263
259
  end
264
260
 
@@ -313,7 +309,7 @@ module Remap
313
309
  # @return [Rule::Each]]
314
310
  # @raise [ArgumentError] if no block given
315
311
  def each(&block)
316
- unless block
312
+ unless block_given?
317
313
  raise ArgumentError, "#each requires a block"
318
314
  end
319
315
 
@@ -346,7 +342,7 @@ module Remap
346
342
  # @return [Rule::Wrap]
347
343
  # @raise [ArgumentError] if type is not :array
348
344
  def wrap(type, &block)
349
- unless block
345
+ unless block_given?
350
346
  raise ArgumentError, "#wrap requires a block"
351
347
  end
352
348
 
@@ -372,8 +368,8 @@ module Remap
372
368
  # output.fetch(:value) # => { names: ["John", "Jane"] }
373
369
  #
374
370
  # @return [Rule::Path::Segment::Quantifier::All]
375
- def all(&block)
376
- if block
371
+ def all
372
+ if block_given?
377
373
  raise ArgumentError, "all selector does not take a block"
378
374
  end
379
375
 
@@ -398,8 +394,8 @@ module Remap
398
394
  # output.fetch(:value) # => { api_key: "<SECRET>" }
399
395
  #
400
396
  # @return [Rule::Static::Fixed]
401
- def value(value, &block)
402
- if block
397
+ def value(value)
398
+ if block_given?
403
399
  raise ArgumentError, "option selector does not take a block"
404
400
  end
405
401
 
@@ -424,8 +420,8 @@ module Remap
424
420
  # @param id [Symbol]
425
421
  #
426
422
  # @return [Rule::Static::Option]
427
- def option(id, backtrace: Kernel.caller, &block)
428
- if block
423
+ def option(id, backtrace: caller)
424
+ if block_given?
429
425
  raise ArgumentError, "option selector does not take a block"
430
426
  end
431
427
 
@@ -453,8 +449,8 @@ module Remap
453
449
  #
454
450
  # @return [Path::Segment::Key]
455
451
  # @raise [ArgumentError] if index is not an Integer
456
- def at(index, &block)
457
- if block
452
+ def at(index)
453
+ if block_given?
458
454
  raise ArgumentError, "first selector does not take a block"
459
455
  end
460
456
 
@@ -482,8 +478,8 @@ module Remap
482
478
  # output.fetch(:value) # => { name: "John" }
483
479
  #
484
480
  # @return [Path::Segment::Key]]
485
- def first(&block)
486
- if block
481
+ def first
482
+ if block_given?
487
483
  raise ArgumentError, "first selector does not take a block"
488
484
  end
489
485
 
@@ -509,8 +505,8 @@ module Remap
509
505
  # output.fetch(:value) # => { name: "Linus" }
510
506
  #
511
507
  # @return [Path::Segment::Key]
512
- def last(&block)
513
- if block
508
+ def last
509
+ if block_given?
514
510
  raise ArgumentError, "last selector does not take a block"
515
511
  end
516
512
 
@@ -523,7 +519,7 @@ module Remap
523
519
  rule.tap { rules << rule }
524
520
  end
525
521
 
526
- def rule(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block)
522
+ def rule(*path, to: EMPTY_ARRAY, backtrace: caller, &block)
527
523
  Rule::Map::Required.call({
528
524
  path: {
529
525
  output: [to].flatten,
@@ -534,7 +530,7 @@ module Remap
534
530
  })
535
531
  end
536
532
 
537
- def rule?(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block)
533
+ def rule?(*path, to: EMPTY_ARRAY, backtrace: caller, &block)
538
534
  Rule::Map::Optional.call({
539
535
  path: {
540
536
  output: [to].flatten,
@@ -28,21 +28,20 @@ module Remap
28
28
  # @return [Any]
29
29
  #
30
30
  # @raise When path cannot be found
31
- def get(*path, &error)
32
- _, result = path.reduce([
33
- EMPTY_ARRAY,
34
- self
35
- ]) do |(current_path, element), key|
36
- value = element.fetch(key) do
37
- raise PathError, current_path + [key]
38
- end
31
+ def get(*path, trace: [], &fallback)
32
+ return self if path.empty?
33
+
34
+ key = path.first
39
35
 
40
- [current_path + [key], value]
41
- rescue TypeError
42
- raise PathError, current_path + [key]
36
+ unless block_given?
37
+ get(*path, trace: trace) do
38
+ raise PathError, trace + [key]
39
+ end
43
40
  end
44
41
 
45
- result
42
+ fetch(key, &fallback).get(*path[1..], trace: trace + [key], &fallback)
43
+ rescue TypeError
44
+ raise PathError, trace + [key]
46
45
  end
47
46
  end
48
47
  end
@@ -17,14 +17,29 @@ module Remap
17
17
  block["Expected a state, got [#{self}] (#{self.class})"]
18
18
  end
19
19
 
20
+ # @return [Array]
21
+ #
22
+ # @see Extension::Paths::Hash
23
+ def paths
24
+ []
25
+ end
26
+
20
27
  # Fallback method used when #get is called on an object that does not respond to #get
21
28
  #
22
29
  # Block is invoked, if provided
23
30
  # Otherwise a symbol is thrown
24
31
  #
25
32
  # @param path [Array<Key>]
26
- def get(*path, &block)
27
- raise PathError, []
33
+ def get(*path, trace: [], &fallback)
34
+ return self if path.empty?
35
+
36
+ unless block_given?
37
+ get(*path, trace: trace) do
38
+ raise PathError, trace
39
+ end
40
+ end
41
+
42
+ yield
28
43
  end
29
44
  alias_method :fetch, :get
30
45
 
data/lib/remap/failure.rb CHANGED
@@ -14,22 +14,21 @@ module Remap
14
14
  # @return [Failure]
15
15
  def merge(other)
16
16
  unless other.is_a?(self.class)
17
- raise ArgumentError, "can't merge #{self.class} with #{other.class}"
17
+ raise ArgumentError, "Cannot merge %s (%s) with %s (%s)" % [
18
+ self, self.class, other, other.class
19
+ ]
18
20
  end
19
21
 
20
- failure = attributes.deep_merge(other.attributes) do |_, value1, value2|
21
- case [value1, value2]
22
- in [Array, Array]
22
+ failure = attributes.deep_merge(other.attributes) do |key, value1, value2|
23
+ case [key, value1, value2]
24
+ in [:failures | :notices, Array, Array]
23
25
  value1 + value2
24
- else
25
- raise ArgumentError, "can't merge #{self.class} with #{other.class}"
26
26
  end
27
27
  end
28
28
 
29
29
  new(failure)
30
30
  end
31
31
 
32
- # @return [String]
33
32
  def exception
34
33
  Error.new(attributes.formated)
35
34
  end
data/lib/remap/proxy.rb CHANGED
@@ -8,6 +8,7 @@ module Remap
8
8
 
9
9
  include Dry::Core::Constants
10
10
  extend Dry::Initializer
11
+ include Kernel
11
12
 
12
13
  # See Object#tap
13
14
  def tap(&block)
@@ -11,11 +11,10 @@ module Remap
11
11
  # index = Remap::Selector::Index.new(1)
12
12
  #
13
13
  # result = index.call(state) do |element|
14
- # element.fmap do |value|
15
- # value.upcase
16
- # end
14
+ # value = element.fetch(:value)
15
+ # element.merge(value: value.upcase)
17
16
  # end
18
-
17
+ #
19
18
  # result.fetch(:value) # => :TWO
20
19
  class Index < Unit
21
20
  # @return [Integer]
@@ -7,13 +7,6 @@ module Remap
7
7
  using Extensions::Object
8
8
  using Extensions::Hash
9
9
 
10
- refine Object do
11
- # @see Extension::Paths::Hash
12
- def paths
13
- EMPTY_ARRAY
14
- end
15
- end
16
-
17
10
  refine Hash do
18
11
  # Returns a list of all key paths
19
12
  #
@@ -23,7 +16,7 @@ module Remap
23
16
  # b: :c
24
17
  # },
25
18
  # d: :e
26
- # }.hur_paths # => [[:a, :b], [:d]]
19
+ # }.paths # => [[:a, :b], [:d]]
27
20
  #
28
21
  # @return [Array<Array<Symbol>>] a list of key paths
29
22
  def paths
@@ -47,7 +40,7 @@ module Remap
47
40
  # b: :c
48
41
  # },
49
42
  # d: :e
50
- # }.hur_only(:a, :b) # => { a: { b: :c } }
43
+ # }.only(:a, :b) # => { a: { b: :c } }
51
44
  #
52
45
  # @returns [Hash] a hash containing the given path
53
46
  # @raise Europace::Error when path doesn't exist
@@ -117,11 +110,7 @@ module Remap
117
110
  def map(&block)
118
111
  bind do |value, state|
119
112
  Iteration.call(state: state, value: value).call do |other, **options|
120
- state.set(other, **options).then do |inner_state|
121
- block[inner_state] do |failure|
122
- throw :failure, failure
123
- end
124
- end
113
+ state.set(other, **options).then(&block)
125
114
  end.except(:index, :element, :key)
126
115
  end
127
116
  end
@@ -275,10 +264,6 @@ module Remap
275
264
  end
276
265
 
277
266
  set(result)._
278
- rescue NoMethodError => e
279
- e.name == :fetch ? error["Fetch not defined on value: #{e}"] : raise
280
- rescue NameError => e
281
- e.name == :Undefined ? error["Undefined returned, skipping!: #{e}"] : raise
282
267
  rescue KeyError, IndexError => e
283
268
  error[e.message]
284
269
  rescue PathError => e
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.1.14
4
+ version: 2.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linus Oleander