Class: Traject::Indexer::EachRecordStep

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

Constant Summary collapse

EMPTY_ACCUMULATOR =
[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lambda, block, source_location) ⇒ EachRecordStep

Returns a new instance of EachRecordStep.



17
18
19
20
21
22
23
# File 'lib/traject/indexer/step.rb', line 17

def initialize(lambda, block, source_location)
  self.lambda          = lambda
  self.block           = block
  self.source_location = source_location

  self.validate!
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



12
13
14
# File 'lib/traject/indexer/step.rb', line 12

def block
  @block
end

#lambdaObject

Returns the value of attribute lambda.



13
14
15
# File 'lib/traject/indexer/step.rb', line 13

def lambda
  @lambda
end

#source_locationObject

Returns the value of attribute source_location.



12
13
14
# File 'lib/traject/indexer/step.rb', line 12

def source_location
  @source_location
end

Instance Method Details

#execute(context) ⇒ Object

For each_record, always return an empty array as the accumulator, since it doesn't have those kinds of side effects



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/traject/indexer/step.rb', line 67

def execute(context)
  sr = context.source_record

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

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

  return EMPTY_ACCUMULATOR # empty -- no accumulator for each_record
end

#inspectObject

Over-ride inspect for outputting error messages etc.



86
87
88
# File 'lib/traject/indexer/step.rb', line 86

def inspect
  "(each_record at #{source_location})"
end

#to_field_step?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/traject/indexer/step.rb', line 25

def to_field_step?
  false
end

#validate!Object

raises if bad data



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/traject/indexer/step.rb', line 45

def validate!
  unless self.lambda or self.block
    raise ArgumentError.new("Missing Argument: each_record must take a block/lambda as an argument (#{self.inspect})")
  end

  [self.lambda, self.block].each do |proc|
    # allow negative arity, meaning variable/optional, trust em on that.
    # but for positive arrity, we need 1 or 2 args
    if proc
      unless proc.is_a?(Proc)
        raise NamingError.new("argument to each_record must be a block/lambda, not a #{proc.class} #{self.inspect}")
      end
      if (proc.arity == 0 || proc.arity > 2)
        raise ArityError.new("block/proc given to each_record needs 1 or 2 arguments: #{self.inspect}")
      end
    end
  end
end