remap 2.2.47 → 2.2.51

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: d745b7e030764a548c8cb8b2651e89cf22485154120bab89053743653d0ccd9a
4
- data.tar.gz: '04408f1457593d3c2f3fb8fc5d52f275b74e9d3f0eb31a4088d073ee93ddeeb6'
3
+ metadata.gz: 9d9b65b616274770efbfd7e0cbc0a1267dad1f362c8b00031b389467099fd7dd
4
+ data.tar.gz: ce414f535c4a5d045b7cda3b3e265ddca8ac100ed2937a55d5e812a033e4beea
5
5
  SHA512:
6
- metadata.gz: 2fb3e7f89ed0dea90a33a33dee5f5a5b358f22de24b24a64e531a7a84196da067e621060ab21045efb810c02cc8af0f53e7ca43d36ddad3124fb1a2212973a81
7
- data.tar.gz: ad67a4ec14390748e9df5d77f8093c44143ae63df023a5ea84434ab79f7c5a894f1df0cdf7b3ce38b19fea44668b19ba269b3185d18d6deaa435a4620f1a733f
6
+ metadata.gz: be33c29fca4e449ce50e40d25ed31153ac076c6ab4fdbeb56ec911f5e6b66a2502b496899a655619bbd230e303320e5b279730e4e412f3b3ef7f720fd1a709b1
7
+ data.tar.gz: 69bdec20f5147b7a9def5404c3a00c73a8a15f247d201189d96ee9b5c345795b9fc41ea3b65ba2244154ab0ade712b8b0aeee7d10d3ddf87737e402b761c9c88
data/lib/remap/base.rb CHANGED
@@ -34,33 +34,33 @@ module Remap
34
34
  # Mapper.call({}) # => { api_key: "ABC-123" }
35
35
  #
36
36
  # @example Maps ["A", "B", "C"] to ["A", "C"]
37
- # class Mapper < Remap::Base
37
+ # class IfNotMapper < Remap::Base
38
38
  # define do
39
39
  # each do
40
- # map?.if_not do
40
+ # map?.if_not do |value|
41
41
  # value.include?("B")
42
42
  # end
43
43
  # end
44
44
  # end
45
45
  # end
46
46
  #
47
- # Mapper.call(["A", "B", "C"]) # => ["A", "C"]
47
+ # IfNotMapper.call(["A", "B", "C"]) # => ["A", "C"]
48
48
  #
49
49
  # @example Maps ["A", "B", "C"] to ["B"]
50
- # class Mapper < Remap::Base
50
+ # class IfMapper < Remap::Base
51
51
  # define do
52
52
  # each do
53
- # map?.if do
53
+ # map?.if do |value|
54
54
  # value.include?("B")
55
55
  # end
56
56
  # end
57
57
  # end
58
58
  # end
59
59
  #
60
- # Mapper.call(["A", "B", "C"]) # => ["B"]
60
+ # IfMapper.call(["A", "B", "C"]) # => ["B"]
61
61
  #
62
62
  # @example Maps { a: { b: "A" } } to "A"
63
- # class Mapper < Remap::Base
63
+ # class EnumMapper < Remap::Base
64
64
  # define do
65
65
  # map(:a, :b).enum do
66
66
  # value "A", "B"
@@ -68,11 +68,11 @@ module Remap
68
68
  # end
69
69
  # end
70
70
  #
71
- # Mapper.call({ a: { b: "A" } }) # => "A"
72
- # Mapper.call({ a: { b: "B" } }) # => "B"
71
+ # EnumMapper.call({ a: { b: "A" } }) # => "A"
72
+ # EnumMapper.call({ a: { b: "B" } }) # => "B"
73
73
  #
74
74
  # @example Map { people: [{ name: "John" }] } to { names: ["John"] }
75
- # class Mapper < Remap::Base
75
+ # class PeopleMapper < Remap::Base
76
76
  # define do
77
77
  # map :people, to: :names do
78
78
  # each do
@@ -82,12 +82,12 @@ module Remap
82
82
  # end
83
83
  # end
84
84
  #
85
- # Mapper.call({ people: [{ name: "John" }] }) # => { names: ["John"] }
85
+ # PeopleMapper.call({ people: [{ name: "John" }] }) # => { names: ["John"] }
86
86
  #
87
87
  # @example Map "Hello" to "Hello!"
88
88
  # class HelloMapper < Remap::Base
89
89
  # define do
90
- # map.adjust do
90
+ # map.adjust do |value|
91
91
  # "#{value}!"
92
92
  # end
93
93
  # end
@@ -16,7 +16,7 @@ module Remap
16
16
  end
17
17
 
18
18
  s3 = s2.then(&path.output)
19
- s4 = s3.set(path: state.path)
19
+ s4 = s3.merge(path: state.path)
20
20
 
21
21
  s4.except(:key)
22
22
  end
@@ -117,7 +117,7 @@ module Remap
117
117
  # Keeps map, only if block is true
118
118
  #
119
119
  # @example Keep if value contains "A"
120
- # map = Remap::Rule::Map::Optional.call(backtrace: caller).if do
120
+ # map = Remap::Rule::Map::Optional.call(backtrace: caller).if do |value|
121
121
  # value.include?("A")
122
122
  # end
123
123
  #
@@ -145,7 +145,7 @@ module Remap
145
145
  #
146
146
 
147
147
  # @example Keep unless value contains "A"
148
- # map = Remap::Rule::Map::Optional.new(backtrace: caller).if_not do
148
+ # map = Remap::Rule::Map::Optional.new(backtrace: caller).if_not do |value|
149
149
  # value.include?("A")
150
150
  # end
151
151
  #
@@ -15,28 +15,28 @@ module Remap
15
15
  # end
16
16
  # result.fetch(:value) # => [Hash, Hash]
17
17
  class All < Concrete
18
- requirement Types::Enumerable
19
-
20
18
  # Iterates over state and passes each value to block
21
19
  #
22
- # @param outer_state [State<Enumerable<T>>]
20
+ # @param state [State<Enumerable<T>>]
23
21
  #
24
22
  # @yieldparam [State<T>]
25
23
  # @yieldreturn [State<U>]
26
24
  #
27
25
  # @return [State<U>]
28
- def call(outer_state, &block)
26
+ def call(state, &block)
29
27
  unless block
30
28
  raise ArgumentError, "All selector requires an iteration block"
31
29
  end
32
30
 
33
- outer_state.bind(quantifier: "*") do |enum, state|
34
- requirement[enum] do
35
- state.fatal!("Expected enumerable")
36
- end
31
+ value = state.fetch(:value) do
32
+ return state
33
+ end
37
34
 
38
- state.map(&block)
35
+ unless value.is_a?(Enumerable)
36
+ state.fatal!("Not an enumerator")
39
37
  end
38
+
39
+ state.map(&block)
40
40
  end
41
41
  end
42
42
  end
@@ -20,8 +20,6 @@ module Remap
20
20
  # @return [Integer]
21
21
  attribute :index, Integer
22
22
 
23
- requirement Types::Array
24
-
25
23
  # Selects the {#index}th element from state and passes it to block
26
24
  #
27
25
  # @param state [State<Array<T>>]
@@ -35,17 +33,17 @@ module Remap
35
33
  raise ArgumentError, "The index selector requires an iteration block"
36
34
  end
37
35
 
38
- state.bind(index: index) do |array, s|
39
- requirement[array] do
40
- s.fatal!("Expected an array")
41
- end
36
+ array = state.fetch(:value) { return state }
42
37
 
43
- element = array.fetch(index) do
44
- s.ignore!("Index not found")
45
- end
38
+ unless array.is_a?(Array)
39
+ state.fatal!("Expected an array got %s", array.class)
40
+ end
46
41
 
47
- state.set(element, index: index).then(&block)
42
+ value = array.fetch(index) do
43
+ state.ignore!("Index [%s] (%s) not found", index, index.class)
48
44
  end
45
+
46
+ state.set(value, index: index).then(&block)
49
47
  end
50
48
  end
51
49
  end
@@ -17,8 +17,6 @@ module Remap
17
17
  # @return [#hash
18
18
  attribute :key, Types::Key
19
19
 
20
- requirement Types::Hash
21
-
22
20
  # Selects {#key} from state and passes it to block
23
21
  #
24
22
  # @param state [State<Hash<K, V>>]
@@ -32,17 +30,17 @@ module Remap
32
30
  raise ArgumentError, "The key selector requires an iteration block"
33
31
  end
34
32
 
35
- state.bind(key: key) do |hash, s|
36
- requirement[hash] do
37
- s.fatal!("Expected hash")
38
- end
33
+ hash = state.fetch(:value) { return state }
39
34
 
40
- value = hash.fetch(key) do
41
- s.ignore!("Key not found")
42
- end
35
+ unless hash.is_a?(Hash)
36
+ state.fatal!("Expected hash got %s", hash.class)
37
+ end
43
38
 
44
- state.set(value, key: key).then(&block)
39
+ value = hash.fetch(key) do
40
+ state.ignore!("Key [%s] (%s) not found", key, key.class)
45
41
  end
42
+
43
+ state.set(value, key: key).then(&block)
46
44
  end
47
45
  end
48
46
  end
@@ -4,9 +4,6 @@ module Remap
4
4
  # Defines how a path element, or selector
5
5
  # Specifies how a value is extracted from a state
6
6
  class Selector < Dry::Interface
7
- defines :requirement, type: Types::Any.constrained(type: Dry::Types::Type)
8
- requirement Types::Any
9
-
10
7
  # Selects value from state, package it as a state and passes it to block
11
8
  #
12
9
  # @param state [State]
@@ -20,12 +17,5 @@ module Remap
20
17
  def call(state)
21
18
  raise NotImplementedError, "#{self.class}#call not implemented"
22
19
  end
23
-
24
- private
25
-
26
- # @return [Dry::Types::Type]
27
- def requirement
28
- self.class.requirement
29
- end
30
20
  end
31
21
  end
@@ -223,6 +223,8 @@ module Remap
223
223
  state.merge!(notices: notices + [notice])
224
224
  in [{value:}, {mapper:}]
225
225
  state.merge!(scope: value, mapper: mapper)
226
+ in [{path: p1}, {path: p2}]
227
+ state.merge!(path: p1 + p2)
226
228
  in [{path:}, {key:, value:}]
227
229
  state.merge!(path: path + [key], key: key, value: value)
228
230
  in [{path:}, {key:}]
@@ -289,25 +291,33 @@ module Remap
289
291
  #
290
292
  # @return [State<U>]
291
293
  def execute(&block)
292
- bind do |value|
293
- result = catch :done do
294
- tail_path = catch :ignore do
295
- throw :done, context(value).instance_exec(value, &block)
296
- rescue KeyError => e
297
- [e.key]
298
- rescue IndexError
299
- []
294
+ value = fetch(:value) { return self }
295
+
296
+ path = catch :ignore do
297
+ names = block.parameters.reduce([]) do |acc, (type, name)|
298
+ case type
299
+ in :keyreq
300
+ acc + [name]
301
+ else
302
+ acc
300
303
  end
301
-
302
- set(path: path + tail_path).ignore!("Undefined path")
303
304
  end
304
305
 
305
- if result.equal?(Dry::Core::Constants::Undefined)
306
- ignore!("Undefined returned, skipping!")
306
+ n1 = options.only(*names)
307
+ n2 = only(*names)
308
+
309
+ result = block[value, **n2, **n1] do |reason|
310
+ return ignore!(reason)
307
311
  end
308
312
 
309
- set(result)
313
+ return set(result)
310
314
  end
315
+
316
+ set(path: path).ignore!("Undefined path")
317
+ rescue KeyError => e
318
+ set(path: [e.key]).ignore!(e.message)
319
+ rescue IndexError => e
320
+ ignore!(e.message)
311
321
  end
312
322
 
313
323
  # Passes {#value} to block and returns {self}
@@ -428,27 +438,6 @@ module Remap
428
438
 
429
439
  throw id, remove_id
430
440
  end
431
-
432
- private
433
-
434
- # Creates a context containing {options} and {self}
435
- #
436
- # @param value [Any]
437
- #
438
- # @yieldparam reason [T]
439
- #
440
- # @return [Struct]
441
- def context(value, context: self)
442
- ::Struct.new(*except(:id).keys, *options.keys, :state, keyword_init: true) do
443
- define_method :method_missing do |name, *|
444
- context.fatal!("Method [%s] not defined", name)
445
- end
446
-
447
- define_method(:skip!) do |message = "Manual skip!"|
448
- context.ignore!(message)
449
- end
450
- end.new(**to_hash, **options, value: value, state: self)
451
- end
452
441
  end
453
442
  end
454
443
  end
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.47
4
+ version: 2.2.51
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-16 00:00:00.000000000 Z
11
+ date: 2021-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -150,76 +150,6 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: 2.5.1
153
- - !ruby/object:Gem::Dependency
154
- name: rake
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- version: '0'
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- version: '0'
167
- - !ruby/object:Gem::Dependency
168
- name: rspec-collection_matchers
169
- requirement: !ruby/object:Gem::Requirement
170
- requirements:
171
- - - ">="
172
- - !ruby/object:Gem::Version
173
- version: '0'
174
- type: :development
175
- prerelease: false
176
- version_requirements: !ruby/object:Gem::Requirement
177
- requirements:
178
- - - ">="
179
- - !ruby/object:Gem::Version
180
- version: '0'
181
- - !ruby/object:Gem::Dependency
182
- name: rubocop
183
- requirement: !ruby/object:Gem::Requirement
184
- requirements:
185
- - - "~>"
186
- - !ruby/object:Gem::Version
187
- version: 1.23.0
188
- type: :development
189
- prerelease: false
190
- version_requirements: !ruby/object:Gem::Requirement
191
- requirements:
192
- - - "~>"
193
- - !ruby/object:Gem::Version
194
- version: 1.23.0
195
- - !ruby/object:Gem::Dependency
196
- name: rubocop-performance
197
- requirement: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - ">="
200
- - !ruby/object:Gem::Version
201
- version: '0'
202
- type: :development
203
- prerelease: false
204
- version_requirements: !ruby/object:Gem::Requirement
205
- requirements:
206
- - - ">="
207
- - !ruby/object:Gem::Version
208
- version: '0'
209
- - !ruby/object:Gem::Dependency
210
- name: rubocop-rake
211
- requirement: !ruby/object:Gem::Requirement
212
- requirements:
213
- - - ">="
214
- - !ruby/object:Gem::Version
215
- version: '0'
216
- type: :development
217
- prerelease: false
218
- version_requirements: !ruby/object:Gem::Requirement
219
- requirements:
220
- - - ">="
221
- - !ruby/object:Gem::Version
222
- version: '0'
223
153
  description: |
224
154
  Re:map; an expressive and feature-rich data transformation mapper for Ruby 3.
225
155
  It gives the developer the expressive power of JSONPath, without the hassle of