1
1
# frozen_string_literal: true
2
2
3
- require 'transproc /all'
3
+ require 'dry/transformer /all'
4
4
5
5
require 'rom/processor'
6
+ require 'rom/processor/composer'
6
7
7
8
module ROM
8
9
class Processor
9
- # Data mapping transformer builder using Transproc
10
+ # Data mapping transformer builder using dry-transformer
10
11
#
11
- # This builds a transproc function that is used to map a whole relation
12
+ # This builds a transformer object that is used to map a whole relation
12
13
#
13
- # @see https://github.com/solnic/transproc too
14
+ # @see https://github.com/dry-rb/dry-transformer
14
15
#
15
16
# @private
16
- class Transproc < Processor
17
- include :: Transproc :: Composer
17
+ class Transformer < Processor
18
+ include Composer
18
19
19
20
module Functions
20
- extend :: Transproc ::Registry
21
+ extend Dry :: Transformer ::Registry
21
22
22
- import ::Transproc ::Coercions
23
- import ::Transproc ::ArrayTransformations
24
- import ::Transproc ::HashTransformations
25
- import ::Transproc ::ClassTransformations
26
- import ::Transproc ::ProcTransformations
27
- INVALID_INJECT_UNION_VALUE = "%s attribute: block is required for :from with union value." . freeze
23
+ import Dry ::Transformer ::Coercions
24
+ import Dry ::Transformer ::ArrayTransformations
25
+ import Dry ::Transformer ::HashTransformations
26
+ import Dry ::Transformer ::ClassTransformations
27
+ import Dry ::Transformer ::ProcTransformations
28
28
29
- def self . identity ( tuple )
30
- tuple
31
- end
29
+ INVALID_INJECT_UNION_VALUE = '%s attribute: block is required for :from with union value.'
32
30
33
31
def self . get ( arr , idx )
34
32
arr [ idx ]
@@ -39,7 +37,7 @@ def self.filter_empty(arr)
39
37
end
40
38
41
39
def self . inject_union_value ( tuple , name , keys , coercer )
42
- raise ROM ::MapperMisconfiguredError , INVALID_INJECT_UNION_VALUE % [ name ] if ! coercer
40
+ raise ROM ::MapperMisconfiguredError , INVALID_INJECT_UNION_VALUE % [ name ] unless coercer
43
41
44
42
values = tuple . values_at ( *keys )
45
43
result = coercer . call ( *values )
@@ -73,15 +71,15 @@ def self.inject_union_value(tuple, name, keys, coercer)
73
71
# @api private
74
72
attr_reader :row_proc
75
73
76
- # Build a transproc function from the header
74
+ # Build a transformer object from the header
77
75
#
78
76
# @param [ROM::Header] header
79
77
#
80
- # @return [Transproc::Function ]
78
+ # @return [Dry::Transformer::Pipe ]
81
79
#
82
80
# @api private
83
81
def self . build ( mapper , header )
84
- new ( mapper , header ) . to_transproc
82
+ new ( mapper , header ) . call
85
83
end
86
84
87
85
# @api private
@@ -93,12 +91,12 @@ def initialize(mapper, header)
93
91
initialize_row_proc
94
92
end
95
93
96
- # Coerce mapper header to a transproc data mapping function
94
+ # Coerce mapper header to a transformer object
97
95
#
98
- # @return [Transproc::Function ]
96
+ # @return [Dry::Transformer::Pipe ]
99
97
#
100
98
# @api private
101
- def to_transproc
99
+ def call
102
100
compose ( t ( :identity ) ) do |ops |
103
101
combined = header . combined
104
102
ops << t ( :combine , combined . map ( &method ( :combined_args ) ) ) if combined . any?
@@ -315,22 +313,22 @@ def visit_fold(attribute, preprocess = false)
315
313
#
316
314
# @api private
317
315
def visit_unfold ( attribute , preprocess = false )
318
- if preprocess
319
- name = attribute . name
320
- header = attribute . header
321
- keys = attribute . pop_keys
322
- key = keys . first
316
+ return unless preprocess
323
317
324
- others = header . postprocessed
318
+ name = attribute . name
319
+ header = attribute . header
320
+ keys = attribute . pop_keys
321
+ key = keys . first
325
322
326
- compose do |ops |
327
- ops << others . map { |attr |
328
- t ( :map_array , t ( :map_value , name , visit ( attr , true ) ) )
329
- }
330
- ops << t ( :map_array , t ( :map_value , name , t ( :insert_key , key ) ) )
331
- ops << t ( :map_array , t ( :reject_keys , [ key ] - [ name ] ) )
332
- ops << t ( :ungroup , name , [ key ] )
333
- end
323
+ others = header . postprocessed
324
+
325
+ compose do |ops |
326
+ ops << others . map { |attr |
327
+ t ( :map_array , t ( :map_value , name , visit ( attr , true ) ) )
328
+ }
329
+ ops << t ( :map_array , t ( :map_value , name , t ( :insert_key , key ) ) )
330
+ ops << t ( :map_array , t ( :reject_keys , [ key ] - [ name ] ) )
331
+ ops << t ( :ungroup , name , [ key ] )
334
332
end
335
333
end
336
334
0 commit comments