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.



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

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.



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

def block
  @block
end

#field_nameObject

Returns the value of attribute field_name.



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

def field_name
  @field_name
end

#lambdaObject

Returns the value of attribute lambda.



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

def lambda
  @lambda
end

#source_locationObject

Returns the value of attribute source_location.



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

def source_location
  @source_location
end

Instance Method Details

#execute(context) ⇒ Object



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

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



496
497
498
# File 'lib/traject/indexer.rb', line 496

def inspect
  "<to_field #{self.field_name} at #{self.source_location}>"
end

#validate!Object



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

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