Class: Traject::Indexer::ToFieldStep

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

Overview

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.

Constant Summary collapse

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.

"allow_nil_values".freeze
ALLOW_EMPTY_FIELDS =
"allow_empty_fields".freeze
ALLOW_DUPLICATE_VALUES =
"allow_duplicate_values".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, procs, block, source_location) ⇒ ToFieldStep

Returns a new instance of ToFieldStep.



98
99
100
101
102
103
104
105
# 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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



96
97
98
# File 'lib/traject/indexer/step.rb', line 96

def block
  @block
end

#field_nameObject (readonly)

Returns the value of attribute field_name.



96
97
98
# File 'lib/traject/indexer/step.rb', line 96

def field_name
  @field_name
end

#procsObject (readonly)

Returns the value of attribute procs.



96
97
98
# File 'lib/traject/indexer/step.rb', line 96

def procs
  @procs
end

#source_locationObject (readonly)

Returns the value of attribute source_location.



96
97
98
# File 'lib/traject/indexer/step.rb', line 96

def source_location
  @source_location
end

Instance Method Details

#add_accumulator_to_context!(accumulator, context) ⇒ Object

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)



159
160
161
162
# 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) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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

#inspectObject

Override inspect for developer debug messages



127
128
129
# File 'lib/traject/indexer/step.rb', line 127

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

#to_field_step?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/traject/indexer/step.rb', line 107

def to_field_step?
  true
end

#validate!Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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