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.



581
582
583
584
585
586
587
# File 'lib/traject/indexer.rb', line 581

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.



579
580
581
# File 'lib/traject/indexer.rb', line 579

def block
  @block
end

#lambdaObject

Returns the value of attribute lambda.



579
580
581
# File 'lib/traject/indexer.rb', line 579

def lambda
  @lambda
end

#source_locationObject

Returns the value of attribute source_location.



579
580
581
# File 'lib/traject/indexer.rb', line 579

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



611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/traject/indexer.rb', line 611

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.



626
627
628
# File 'lib/traject/indexer.rb', line 626

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

#validate!Object

raises if bad data



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/traject/indexer.rb', line 590

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