class Traject::Indexer::ToFieldStep
An indexing step definition for a “to_field” step to specific field. The first field name argument can be an array of multiple field names, the processed values will be added to each one.
Constants
- ALLOW_DUPLICATE_VALUES
- ALLOW_EMPTY_FIELDS
- ALLOW_NIL_VALUES
These constqnts here for historical/legacy reasons, they really oughta live in Traject::Context, but in case anyone is referring to them we'll leave them here for now.
Attributes
block[R]
field_name[R]
procs[R]
source_location[R]
Public Class Methods
new(field_name, procs, block, source_location)
click to toggle source
# File lib/traject/indexer/step.rb, line 98 def initialize(field_name, procs, block, source_location) @field_name = field_name.freeze @procs = procs.freeze @block = block.freeze @source_location = source_location.freeze validate! end
Public Instance Methods
add_accumulator_to_context!(accumulator, context)
click to toggle source
Add the accumulator to the context with the correct field name(s). Do post-processing on the accumulator (remove nil values, allow empty fields, etc)
# File lib/traject/indexer/step.rb, line 159 def add_accumulator_to_context!(accumulator, context) # field_name can actually be an array of field names context.add_output(field_name, *accumulator) end
execute(context)
click to toggle source
# File lib/traject/indexer/step.rb, line 131 def execute(context) accumulator = [] source_record = context.source_record [*self.procs, self.block].each do |aProc| next unless aProc if aProc.arity == 2 aProc.call(source_record, accumulator) else aProc.call(source_record, accumulator, context) end end add_accumulator_to_context!(accumulator, context) return accumulator end
inspect()
click to toggle source
Override inspect for developer debug messages
# File lib/traject/indexer/step.rb, line 127 def inspect "(to_field #{self.field_name.inspect} at #{self.source_location})" end
to_field_step?()
click to toggle source
# File lib/traject/indexer/step.rb, line 107 def to_field_step? true end
validate!()
click to toggle source
# File lib/traject/indexer/step.rb, line 111 def validate! unless (field_name.is_a?(String) && ! field_name.empty?) || (field_name.is_a?(Array) && field_name.all? { |f| f.is_a?(String) && ! f.empty? }) raise NamingError.new("to_field requires the field name (as a string), or an array of such, as the first argument at #{self.source_location})") end [*self.procs, 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