Class: Traject::Indexer::ToFieldStep

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

Overview

An indexing step definition for a "to_field" step to specific field.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fieldname, lambda, block, source_location) ⇒ ToFieldStep

Returns a new instance of ToFieldStep.



526
527
528
529
530
531
532
533
# File 'lib/traject/indexer.rb', line 526

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

  validate!
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



525
526
527
# File 'lib/traject/indexer.rb', line 525

def block
  @block
end

#field_nameObject

Returns the value of attribute field_name.



525
526
527
# File 'lib/traject/indexer.rb', line 525

def field_name
  @field_name
end

#lambdaObject

Returns the value of attribute lambda.



525
526
527
# File 'lib/traject/indexer.rb', line 525

def lambda
  @lambda
end

#source_locationObject

Returns the value of attribute source_location.



525
526
527
# File 'lib/traject/indexer.rb', line 525

def source_location
  @source_location
end

Instance Method Details

#execute(context) ⇒ Object



555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/traject/indexer.rb', line 555

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

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

  end
  return accumulator
end

#inspectObject

Override inspect for developer debug messages



551
552
553
# File 'lib/traject/indexer.rb', line 551

def inspect
  "(to_field #{self.field_name} at #{self.source_location})"
end

#validate!Object



535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/traject/indexer.rb', line 535

def validate!

  if self.field_name.nil? || !self.field_name.is_a?(String) || self.field_name.empty?
    raise NamingError.new("to_field requires the field name (as a string) as the first argument at #{self.source_location})")
  end

  [self.lambda, self.block].each do |proc|
    # allow negative arity, meaning variable/optional, trust em on that.
    # but for positive arrity, we need 2 or 3 args
    if proc && (proc.arity == 0 || proc.arity == 1 || proc.arity > 3)
      raise ArityError.new("error parsing field '#{self.field_name}': block/proc given to to_field needs 2 or 3 (or variable) arguments: #{proc} (#{self.inspect})")
    end
  end
end