Class: ROM::Processor::Transproc

Inherits:
ROM::Processor show all
Includes:
Transproc::Composer
Defined in:
lib/rom/processor/transproc.rb

Overview

Data mapping transformer builder using Transproc

This builds a transproc function that is used to map a whole relation

rubocop:disable Metrics/ClassLength

See Also:

Defined Under Namespace

Modules: Functions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ROM::Processor

inherited

Constructor Details

#initialize(mapper, header) ⇒ Transproc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Transproc.



87
88
89
90
91
92
93
94
# File 'lib/rom/processor/transproc.rb', line 87

def initialize(mapper, header)
  super()
  @mapper = mapper
  @header = header
  @model = header.model
  @mapping = header.mapping
  initialize_row_proc
end

Instance Attribute Details

#headerHeader (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns header from a mapper.

Returns:

  • (Header)

    header from a mapper



58
59
60
# File 'lib/rom/processor/transproc.rb', line 58

def header
  @header
end

#mapperMapper (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns mapper that this processor belongs to.

Returns:

  • (Mapper)

    mapper that this processor belongs to



53
54
55
# File 'lib/rom/processor/transproc.rb', line 53

def mapper
  @mapper
end

#mappingHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns header’s attribute mapping.

Returns:

  • (Hash)

    header’s attribute mapping



68
69
70
# File 'lib/rom/processor/transproc.rb', line 68

def mapping
  @mapping
end

#modelClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns model class from a mapper.

Returns:

  • (Class)

    model class from a mapper



63
64
65
# File 'lib/rom/processor/transproc.rb', line 63

def model
  @model
end

#row_procProc (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns row-processing proc.

Returns:

  • (Proc)

    row-processing proc



73
74
75
# File 'lib/rom/processor/transproc.rb', line 73

def row_proc
  @row_proc
end

Class Method Details

.build(mapper, header) ⇒ Transproc::Function

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build a transproc function from the header

Parameters:

Returns:

  • (Transproc::Function)


82
83
84
# File 'lib/rom/processor/transproc.rb', line 82

def self.build(mapper, header)
  new(mapper, header).to_transproc
end

Instance Method Details

#to_transprocTransproc::Function

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce mapper header to a transproc data mapping function

rubocop:disable Metrics/AbcSize

Returns:

  • (Transproc::Function)


102
103
104
105
106
107
108
109
110
# File 'lib/rom/processor/transproc.rb', line 102

def to_transproc
  compose(t(:identity)) do |ops|
    combined = header.combined
    ops << t(:combine, combined.map(&method(:combined_args))) if combined.any?
    ops << header.preprocessed.map { |attr| visit(attr, true) }
    ops << t(:map_array, row_proc) if row_proc
    ops << header.postprocessed.map { |attr| visit(attr, true) }
  end
end