Method: Traject::Macros::Transformation#transform

Defined in:
lib/traject/macros/transformation.rb

#transform(a_proc = nil, &block) ⇒ Object

Pass in a proc/lambda arg or a block (or both), that will be called on each value already in the accumulator, to transform it. (Ie, with #map!/#collect! on your proc(s)).

Due to how ruby syntax precedence works, the block form is probably not too useful in traject config files, except with the &: trick.

The "stabby lambda" may be convenient for passing an explicit proc argument.

You can pass both an explicit proc arg and a block, in which case the proc arg will be applied first.

Examples:

to_field("something"), extract_marc("something"), transform(&:upcase)
to_field("something"), extract_marc("something"), transform(->(val) { val.tr('^a-z', "\uFFFD") })


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/traject/macros/transformation.rb', line 60

def transform(a_proc=nil, &block)
  unless a_proc || block
    raise ArgumentError, "Needs a transform proc arg or block arg"
  end

  transformer_callable = if a_proc && block
    # need to make a combo wrapper.
    ->(val) { block.call(a_proc.call(val)) }
  elsif a_proc
    a_proc
  else
    block
  end

  lambda do |rec, acc|
    acc.collect! do |value|
      transformer_callable.call(value)
    end
  end
end