class Traject::Indexer::EachRecordStep

Constants

EMPTY_ACCUMULATOR

Attributes

block[RW]
lambda[R]
source_location[RW]

Public Class Methods

new(lambda, block, source_location) click to toggle source
# File lib/traject/indexer/step.rb, line 17
def initialize(lambda, block, source_location)
  self.lambda          = lambda
  self.block           = block
  self.source_location = source_location

  self.validate!
end

Public Instance Methods

execute(context) click to toggle source

For each_record, always return an empty array as the accumulator, since it doesn't have those kinds of side effects

# File lib/traject/indexer/step.rb, line 67
def execute(context)
  sr = context.source_record

  if @lambda
    if @lambda_arity == 1
      @lambda.call(sr)
    else
      @lambda.call(sr, context)
    end
  end

  if @block
    @block.call(sr, context)
  end

  return EMPTY_ACCUMULATOR # empty -- no accumulator for each_record
end
inspect() click to toggle source

Over-ride inspect for outputting error messages etc.

# File lib/traject/indexer/step.rb, line 86
def inspect
  "(each_record at #{source_location})"
end
lambda=(lam) click to toggle source

Set the arity of the lambda expression just once, when we define it

# File lib/traject/indexer/step.rb, line 31
def lambda=(lam)
  @lambda_arity = 0 # assume
  @lambda = lam

  return unless lam

  if @lambda.is_a?(Proc)
    @lambda_arity = @lambda.arity
  else
    raise NamingError.new("argument to each_record must be a block/lambda, not a #{lam.class} #{self.inspect}")
  end
end
to_field_step?() click to toggle source
# File lib/traject/indexer/step.rb, line 25
def to_field_step?
  false
end
validate!() click to toggle source

raises if bad data

# File lib/traject/indexer/step.rb, line 45
def validate!
  unless self.lambda or self.block
    raise ArgumentError.new("Missing Argument: each_record must take a block/lambda as an argument (#{self.inspect})")
  end

  [self.lambda, self.block].each do |proc|
    # allow negative arity, meaning variable/optional, trust em on that.
    # but for positive arrity, we need 1 or 2 args
    if proc
      unless proc.is_a?(Proc)
        raise NamingError.new("argument to each_record must be a block/lambda, not a #{proc.class} #{self.inspect}")
      end
      if (proc.arity == 0 || proc.arity > 2)
        raise ArityError.new("block/proc given to each_record needs 1 or 2 arguments: #{self.inspect}")
      end
    end
  end
end