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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fieldname, lambda, 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(fieldname, lambda, block, source_location)
  self.field_name      = fieldname.freeze
  self.lambda          = lambda
  self.block           = block
  self.source_location = source_location

  validate!
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



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

def block
  @block
end

#field_nameObject

Returns the value of attribute field_name.



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

def field_name
  @field_name
end

#lambdaObject

Returns the value of attribute lambda.



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

def lambda
  @lambda
end

#source_locationObject

Returns the value of attribute source_location.



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

def source_location
  @source_location
end

Instance Method Details

#execute(context) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/traject/indexer/step.rb', line 136

def execute(context)
  accumulator = []
  sr          = context.source_record

  if @lambda
    if @lambda_arity == 2
      @lambda.call(sr, accumulator)
    else
      @lambda.call(sr, accumulator, context)
    end
  end

  if @block
    @block.call(sr, accumulator, context)
  end


  return accumulator
end

#inspectObject

Override inspect for developer debug messages



132
133
134
# File 'lib/traject/indexer/step.rb', line 132

def inspect
  "(to_field #{self.field_name} 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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/traject/indexer/step.rb', line 116

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