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 +4 -4
- data/lib/remap/base.rb +12 -12
- data/lib/remap/rule/map/required.rb +1 -1
- data/lib/remap/rule/map.rb +2 -2
- data/lib/remap/selector/all.rb +9 -9
- data/lib/remap/selector/index.rb +8 -10
- data/lib/remap/selector/key.rb +8 -10
- data/lib/remap/selector.rb +0 -10
- data/lib/remap/state/extension.rb +23 -34
- metadata +2 -72
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d9b65b616274770efbfd7e0cbc0a1267dad1f362c8b00031b389467099fd7dd
|
4
|
+
data.tar.gz: ce414f535c4a5d045b7cda3b3e265ddca8ac100ed2937a55d5e812a033e4beea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
-
#
|
47
|
+
# IfNotMapper.call(["A", "B", "C"]) # => ["A", "C"]
|
48
48
|
#
|
49
49
|
# @example Maps ["A", "B", "C"] to ["B"]
|
50
|
-
# class
|
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
|
-
#
|
60
|
+
# IfMapper.call(["A", "B", "C"]) # => ["B"]
|
61
61
|
#
|
62
62
|
# @example Maps { a: { b: "A" } } to "A"
|
63
|
-
# class
|
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
|
-
#
|
72
|
-
#
|
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
|
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
|
-
#
|
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
|
data/lib/remap/rule/map.rb
CHANGED
@@ -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
|
#
|
data/lib/remap/selector/all.rb
CHANGED
@@ -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
|
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(
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
31
|
+
value = state.fetch(:value) do
|
32
|
+
return state
|
33
|
+
end
|
37
34
|
|
38
|
-
|
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
|
data/lib/remap/selector/index.rb
CHANGED
@@ -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.
|
39
|
-
requirement[array] do
|
40
|
-
s.fatal!("Expected an array")
|
41
|
-
end
|
36
|
+
array = state.fetch(:value) { return state }
|
42
37
|
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
unless array.is_a?(Array)
|
39
|
+
state.fatal!("Expected an array got %s", array.class)
|
40
|
+
end
|
46
41
|
|
47
|
-
|
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
|
data/lib/remap/selector/key.rb
CHANGED
@@ -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.
|
36
|
-
requirement[hash] do
|
37
|
-
s.fatal!("Expected hash")
|
38
|
-
end
|
33
|
+
hash = state.fetch(:value) { return state }
|
39
34
|
|
40
|
-
|
41
|
-
|
42
|
-
|
35
|
+
unless hash.is_a?(Hash)
|
36
|
+
state.fatal!("Expected hash got %s", hash.class)
|
37
|
+
end
|
43
38
|
|
44
|
-
|
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
|
data/lib/remap/selector.rb
CHANGED
@@ -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
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
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
|
-
|
306
|
-
|
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.
|
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-
|
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
|