remap 2.1.0 → 2.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/remap/compiler.rb +277 -1
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3b3344d7c0ab3825ba1bb0a80c5ddbd7a00d57dbf762b0e26cd3cc6216e0644
4
- data.tar.gz: 30db4e775e16a7923c46d4f1ca41b0b207ab0bb21cf791179a8fd76cbad9d455
3
+ metadata.gz: 6b79008354032e3417442fc906736d8837942692da12022111ded8bdf43ed254
4
+ data.tar.gz: 4c8cc74b50b7549fb79749c378f6cc4507fc3628d83519a791ce2c9ede7686fc
5
5
  SHA512:
6
- metadata.gz: e590c6d048e6cbb822c6bc49428f05e0dd6ead0f0f8752d563b5660adf422e61f1f8e2dc899490fedbdeb34bb36e3dc047906b7fe7eacf9a2711021c6f4a1d5f
7
- data.tar.gz: 58ebb654582cbcfbfc776fb90a8ff2ce88a840642f1c1669c755478cb582f254f7f41a844762f7a228ed8a812e320aab073836dba190a15a3f0a42406a8740a0
6
+ metadata.gz: c710ac63224b18a156d0576e1116622e04ebd4ca51992c7736fbe38390c40737da90df958e75ff8b42ec2bacfaaac4827a86f68b6049cdf027ba4beabb8cb52c
7
+ data.tar.gz: 18270d4afa3d650343b535ac915580557e326ed42bbc9b7aa4be00e414935c38f0da4fd5e2e8da55136dd900ac4245f8aa9e98c9f79162908fb7f1ff6d35beca
@@ -11,7 +11,7 @@ module Remap
11
11
 
12
12
  # Constructs a rule tree given block
13
13
  #
14
- # @example Compiles two rules
14
+ # @example Compiles two rules, [get] and [map]
15
15
  # rule = Remap::Compiler.call do
16
16
  # get :name
17
17
  # get :age
@@ -42,6 +42,21 @@ module Remap
42
42
  # @param path ([]) [Array<Segment>, Segment]
43
43
  # @param to ([]) [Array<Symbol>, Symbol]
44
44
  #
45
+ # @example From path [:name] to [:nickname]
46
+ # rule = Remap::Compiler.call do
47
+ # map :name, to: :nickname
48
+ # end
49
+ #
50
+ # state = Remap::State.call({
51
+ # name: "John"
52
+ # })
53
+ #
54
+ # output = rule.call(state) do |failure|
55
+ # raise failure.exception
56
+ # end
57
+ #
58
+ # output.fetch(:value) # => { nickname: "John" }
59
+ #
45
60
  # @return [Rule::Map::Required]
46
61
  def map(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block)
47
62
  add Rule::Map::Required.call(
@@ -55,6 +70,24 @@ module Remap
55
70
 
56
71
  # Optional version of {#map}
57
72
  #
73
+ # @example Map an optional field
74
+ # rule = Remap::Compiler.call do
75
+ # to :person do
76
+ # map? :age, to: :age
77
+ # map :name, to: :name
78
+ # end
79
+ # end
80
+ #
81
+ # state = Remap::State.call({
82
+ # name: "John"
83
+ # })
84
+ #
85
+ # output = rule.call(state) do |failure|
86
+ # raise failure.exception
87
+ # end
88
+ #
89
+ # output.fetch(:value) # => { person: { name: "John" } }
90
+ #
58
91
  # @see #map
59
92
  #
60
93
  # @return [Rule::Map::Optional]
@@ -72,6 +105,21 @@ module Remap
72
105
  #
73
106
  # @param path ([]) [Array<Segment>, Segment]
74
107
  #
108
+ # @example Map from [:name] to [:name]
109
+ # rule = Remap::Compiler.call do
110
+ # get :name
111
+ # end
112
+ #
113
+ # state = Remap::State.call({
114
+ # name: "John"
115
+ # })
116
+ #
117
+ # output = rule.call(state) do |failure|
118
+ # raise failure.exception
119
+ # end
120
+ #
121
+ # output.fetch(:value) # => { name: "John" }
122
+ #
75
123
  # @return [Rule::Map::Required]
76
124
  def get(*path, backtrace: Kernel.caller, &block)
77
125
  map(path, to: path, backtrace: backtrace, &block)
@@ -79,6 +127,22 @@ module Remap
79
127
 
80
128
  # Optional version of {#get}
81
129
  #
130
+ # @example Map from [:name] to [:name]
131
+ # rule = Remap::Compiler.call do
132
+ # get :name
133
+ # get? :age
134
+ # end
135
+ #
136
+ # state = Remap::State.call({
137
+ # name: "John"
138
+ # })
139
+ #
140
+ # output = rule.call(state) do |failure|
141
+ # raise failure.exception
142
+ # end
143
+ #
144
+ # output.fetch(:value) # => { name: "John" }
145
+ #
82
146
  # @see #get
83
147
  #
84
148
  # @return [Rule::Map::Optional]
@@ -90,6 +154,35 @@ module Remap
90
154
  #
91
155
  # @param mapper [Remap]
92
156
  #
157
+ # @example Embed mapper Car into a person
158
+ # class Car < Remap::Base
159
+ # define do
160
+ # map :car do
161
+ # map :name, to: :car
162
+ # end
163
+ # end
164
+ # end
165
+ #
166
+ # rule = Remap::Compiler.call do
167
+ # map :person do
168
+ # embed Car
169
+ # end
170
+ # end
171
+ #
172
+ # state = Remap::State.call({
173
+ # person: {
174
+ # car: {
175
+ # name: "Volvo"
176
+ # }
177
+ # }
178
+ # })
179
+ #
180
+ # output = rule.call(state) do |failure|
181
+ # raise failure.exception
182
+ # end
183
+ #
184
+ # output.fetch(:value) # => { car: "Volvo" }
185
+ #
93
186
  # @return [Rule::Embed]
94
187
  def embed(mapper, &block)
95
188
  if block
@@ -101,9 +194,37 @@ module Remap
101
194
  raise ArgumentError, "Embeded mapper must be [Remap::Mapper], got [#{mapper}]"
102
195
  end
103
196
 
197
+ # Set a static value
198
+ #
104
199
  # @param path ([]) [Symbol, Array<Symbol>]
105
200
  # @option to [Remap::Static]
106
201
  #
202
+ # @example Set static value to { name: "John" }
203
+ # rule = Remap::Compiler.call do
204
+ # set :name, to: value("John")
205
+ # end
206
+ #
207
+ # state = Remap::State.call({})
208
+ #
209
+ # output = rule.call(state) do |failure|
210
+ # raise failure.exception
211
+ # end
212
+ #
213
+ # output.fetch(:value) # => { name: "John" }
214
+ #
215
+ # @example Reference an option
216
+ # rule = Remap::Compiler.call do
217
+ # set :name, to: option(:name)
218
+ # end
219
+ #
220
+ # state = Remap::State.call({}, options: { name: "John" })
221
+ #
222
+ # output = rule.call(state) do |failure|
223
+ # raise failure.exception
224
+ # end
225
+ #
226
+ # output.fetch(:value) # => { name: "John" }
227
+ #
107
228
  # @return [Rule::Set]
108
229
  # @raise [ArgumentError]
109
230
  # if no path given
@@ -123,6 +244,21 @@ module Remap
123
244
  # @param path [Array<Symbol>, Symbol]
124
245
  # @param map [Array<Segment>, Segment]
125
246
  #
247
+ # @example From path [:name] to [:nickname]
248
+ # rule = Remap::Compiler.call do
249
+ # to :nickname, map: :name
250
+ # end
251
+ #
252
+ # state = Remap::State.call({
253
+ # name: "John"
254
+ # })
255
+ #
256
+ # output = rule.call(state) do |failure|
257
+ # raise failure.exception
258
+ # end
259
+ #
260
+ # output.fetch(:value) # => { nickname: "John" }
261
+ #
126
262
  # @return [Rule::Map]
127
263
  def to(*path, map: EMPTY_ARRAY, backtrace: Kernel.caller, &block)
128
264
  map(*map, to: path, backtrace: backtrace, &block)
@@ -130,6 +266,23 @@ module Remap
130
266
 
131
267
  # Optional version of {#to}
132
268
  #
269
+ # @example Map an optional field
270
+ # rule = Remap::Compiler.call do
271
+ # to :person do
272
+ # to? :age, map: :age
273
+ # to :name, map: :name
274
+ # end
275
+ # end
276
+ #
277
+ # state = Remap::State.call({
278
+ # name: "John"
279
+ # })
280
+ #
281
+ # output = rule.call(state) do |failure|
282
+ # raise failure.exception
283
+ # end
284
+ #
285
+ # output.fetch(:value) # => { person: { name: "John" } }
133
286
  # @see #to
134
287
  #
135
288
  # @return [Rule::Map::Optional]
@@ -140,6 +293,25 @@ module Remap
140
293
  # Iterates over the input value, passes each value
141
294
  # to its block and merges the result back together
142
295
  #
296
+ # @example Map an array of hashes
297
+ # rule = Remap::Compiler.call do
298
+ # each do
299
+ # map :name
300
+ # end
301
+ # end
302
+ #
303
+ # state = Remap::State.call([{
304
+ # name: "John"
305
+ # }, {
306
+ # name: "Jane"
307
+ # }])
308
+ #
309
+ # output = rule.call(state) do |failure|
310
+ # raise failure.exception
311
+ # end
312
+ #
313
+ # output.fetch(:value) # => ["John", "Jane"]
314
+ #
143
315
  # @return [Rule::Each]]
144
316
  # @raise [ArgumentError] if no block given
145
317
  def each(&block)
@@ -156,6 +328,23 @@ module Remap
156
328
  #
157
329
  # @yieldreturn [Rule]
158
330
  #
331
+ # @example Wrap an output value in an array
332
+ # rule = Remap::Compiler.call do
333
+ # wrap(:array) do
334
+ # map :name
335
+ # end
336
+ # end
337
+ #
338
+ # state = Remap::State.call({
339
+ # name: "John"
340
+ # })
341
+ #
342
+ # output = rule.call(state) do |failure|
343
+ # raise failure.exception
344
+ # end
345
+ #
346
+ # output.fetch(:value) # => ["John"]
347
+ #
159
348
  # @return [Rule::Wrap]
160
349
  # @raise [ArgumentError] if type is not :array
161
350
  def wrap(type, &block)
@@ -170,6 +359,22 @@ module Remap
170
359
 
171
360
  # Selects all elements
172
361
  #
362
+ # @example Select all keys in array
363
+ # rule = Remap::Compiler.call do
364
+ # map all, :name, to: :names
365
+ # end
366
+ #
367
+ # state = Remap::State.call([
368
+ # { name: "John" },
369
+ # { name: "Jane" }
370
+ # ])
371
+ #
372
+ # output = rule.call(state) do |failure|
373
+ # raise failure.exception
374
+ # end
375
+ #
376
+ # output.fetch(:value) # => { names: ["John", "Jane"] }
377
+ #
173
378
  # @return [Rule::Path::Segment::Quantifier::All]
174
379
  def all(&block)
175
380
  if block
@@ -183,6 +388,19 @@ module Remap
183
388
  #
184
389
  # @param value [Any]
185
390
  #
391
+ # @example Set path to static value
392
+ # rule = Remap::Compiler.call do
393
+ # set :api_key, to: value("<SECRET>")
394
+ # end
395
+ #
396
+ # state = Remap::State.call({})
397
+ #
398
+ # output = rule.call(state) do |failure|
399
+ # raise failure.exception
400
+ # end
401
+ #
402
+ # output.fetch(:value) # => { api_key: "<SECRET>" }
403
+ #
186
404
  # @return [Rule::Static::Fixed]
187
405
  def value(value, &block)
188
406
  if block
@@ -194,6 +412,19 @@ module Remap
194
412
 
195
413
  # Static option to be selected
196
414
  #
415
+ # @example Set path to option
416
+ # rule = Remap::Compiler.call do
417
+ # set :meaning_of_life, to: option(:number)
418
+ # end
419
+ #
420
+ # state = Remap::State.call({}, options: { number: 42 })
421
+ #
422
+ # output = rule.call(state) do |failure|
423
+ # raise failure.exception
424
+ # end
425
+ #
426
+ # output.fetch(:value) # => { meaning_of_life: 42 }
427
+ #
197
428
  # @param id [Symbol]
198
429
  #
199
430
  # @return [Rule::Static::Option]
@@ -209,6 +440,21 @@ module Remap
209
440
  #
210
441
  # @param index [Integer]
211
442
  #
443
+ # @example Select value at index
444
+ # rule = Remap::Compiler.call do
445
+ # map :names, at(1), to: :name
446
+ # end
447
+ #
448
+ # state = Remap::State.call({
449
+ # names: ["John", "Jane"]
450
+ # })
451
+ #
452
+ # output = rule.call(state) do |failure|
453
+ # raise failure.exception
454
+ # end
455
+ #
456
+ # output.fetch(:value) # => { name: "Jane" }
457
+ #
212
458
  # @return [Path::Segment::Key]
213
459
  # @raise [ArgumentError] if index is not an Integer
214
460
  def at(index, &block)
@@ -224,6 +470,21 @@ module Remap
224
470
 
225
471
  # Selects first element in input
226
472
  #
473
+ # @example Select first value in an array
474
+ # rule = Remap::Compiler.call do
475
+ # map :names, first, to: :name
476
+ # end
477
+ #
478
+ # state = Remap::State.call({
479
+ # names: ["John", "Jane"]
480
+ # })
481
+ #
482
+ # output = rule.call(state) do |failure|
483
+ # raise failure.exception
484
+ # end
485
+ #
486
+ # output.fetch(:value) # => { name: "John" }
487
+ #
227
488
  # @return [Path::Segment::Key]]
228
489
  def first(&block)
229
490
  if block
@@ -236,6 +497,21 @@ module Remap
236
497
 
237
498
  # Selects last element in input
238
499
  #
500
+ # @example Select last value in an array
501
+ # rule = Remap::Compiler.call do
502
+ # map :names, last, to: :name
503
+ # end
504
+ #
505
+ # state = Remap::State.call({
506
+ # names: ["John", "Jane", "Linus"]
507
+ # })
508
+ #
509
+ # output = rule.call(state) do |failure|
510
+ # raise failure.exception
511
+ # end
512
+ #
513
+ # output.fetch(:value) # => { name: "Linus" }
514
+ #
239
515
  # @return [Path::Segment::Key]
240
516
  def last(&block)
241
517
  if block
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.0
4
+ version: 2.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linus Oleander