Class: Traject::Indexer::EachRecordStep

Inherits:
Object
  • Object
show all
Defined in:
lib/traject/indexer.rb

Overview

An indexing step definition, including it's source location for logging

This one represents an "each_record" step, a subclass below for "to_field"

source_location is just a string with filename and line number for showing to devs in debugging.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lambda, block, source_location) ⇒ EachRecordStep

Returns a new instance of EachRecordStep.



471
472
473
474
475
476
477
# File 'lib/traject/indexer.rb', line 471

def initialize(lambda, block, source_location)
  self.lambda = lambda
  self.block = block
  self.source_location = source_location

  self.validate!
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



469
470
471
# File 'lib/traject/indexer.rb', line 469

def block
  @block
end

#lambdaObject

Returns the value of attribute lambda.



469
470
471
# File 'lib/traject/indexer.rb', line 469

def lambda
  @lambda
end

#source_locationObject

Returns the value of attribute source_location.



469
470
471
# File 'lib/traject/indexer.rb', line 469

def source_location
  @source_location
end

Instance Method Details

#execute(context) ⇒ Object

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



501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/traject/indexer.rb', line 501

def execute(context)
  [@lambda, @block].each do |aProc|
    next unless aProc

    if aProc.arity == 1
      aProc.call(context.source_record)
    else
      aProc.call(context.source_record, context)
    end

  end
  return [] # empty -- no accumulator for each_record
end

#inspectObject

Over-ride inspect for outputting error messages etc.



516
517
518
# File 'lib/traject/indexer.rb', line 516

def inspect
  "(each_record at #{source_location})"
end

#validate!Object

raises if bad data



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/traject/indexer.rb', line 480

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