Class: Traject::Indexer::EachRecordStep
- Inherits:
-
Object
- Object
- Traject::Indexer::EachRecordStep
- Defined in:
- lib/traject/indexer.rb
Overview
An indexing step definition, including it’s source location for logging
This one represents an “each_record” step, a subclass below for “to_field”
source_location is just a string with filename and line number for showing to devs in debugging.
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#lambda ⇒ Object
Returns the value of attribute lambda.
-
#source_location ⇒ Object
Returns the value of attribute source_location.
Instance Method Summary collapse
-
#execute(context) ⇒ Object
For each_record, always return an empty array as the accumulator, since it doesn’t have those kinds of side effects.
-
#initialize(lambda, block, source_location) ⇒ EachRecordStep
constructor
A new instance of EachRecordStep.
-
#inspect ⇒ Object
Over-ride inspect for outputting error messages etc.
-
#validate! ⇒ Object
raises if bad data.
Constructor Details
#initialize(lambda, block, source_location) ⇒ EachRecordStep
Returns a new instance of EachRecordStep.
416 417 418 419 420 421 422 |
# File 'lib/traject/indexer.rb', line 416 def initialize(lambda, block, source_location) self.lambda = lambda self.block = block self.source_location = source_location self.validate! end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
414 415 416 |
# File 'lib/traject/indexer.rb', line 414 def block @block end |
#lambda ⇒ Object
Returns the value of attribute lambda.
414 415 416 |
# File 'lib/traject/indexer.rb', line 414 def lambda @lambda end |
#source_location ⇒ Object
Returns the value of attribute source_location.
414 415 416 |
# File 'lib/traject/indexer.rb', line 414 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
446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/traject/indexer.rb', line 446 def execute(context) [@lambda, @block].each do |aProc| next unless aProc if aProc.arity == 1 aProc.call(context.source_record) else aProc.call(context.source_record, context) end end return [] # empty -- no accumulator for each_record end |
#inspect ⇒ Object
Over-ride inspect for outputting error messages etc.
461 462 463 |
# File 'lib/traject/indexer.rb', line 461 def inspect "<each_record at #{source_location}>" end |
#validate! ⇒ Object
raises if bad data
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/traject/indexer.rb', line 425 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 |