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.



636
637
638
639
640
641
642
643
# File 'lib/traject/indexer.rb', line 636

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.



635
636
637
# File 'lib/traject/indexer.rb', line 635

def block
  @block
end

#field_nameObject

Returns the value of attribute field_name.



635
636
637
# File 'lib/traject/indexer.rb', line 635

def field_name
  @field_name
end

#lambdaObject

Returns the value of attribute lambda.



635
636
637
# File 'lib/traject/indexer.rb', line 635

def lambda
  @lambda
end

#source_locationObject

Returns the value of attribute source_location.



635
636
637
# File 'lib/traject/indexer.rb', line 635

def source_location
  @source_location
end

Instance Method Details

#execute(context) ⇒ Object



665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/traject/indexer.rb', line 665

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



661
662
663
# File 'lib/traject/indexer.rb', line 661

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

#validate!Object



645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/traject/indexer.rb', line 645

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