Class: TrajectPlus::Indexer::ComposeStep

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ToFieldStep

add_accumulator_to_context!, #add_accumulator_to_context!, #single?, #to_field_step?

Constructor Details

#initialize(fieldname, procs, block, source_location, indexer) ⇒ ComposeStep

Returns a new instance of ComposeStep.



44
45
46
47
48
49
50
# File 'lib/traject_plus/indexer/step.rb', line 44

def initialize(fieldname, procs, block, source_location, indexer)
  @indexer             = indexer
  @field_name          = fieldname
  @procs               = procs
  @block               = block
  @source_location     = source_location
end

Instance Attribute Details

#indexerObject (readonly)

Returns the value of attribute indexer.



42
43
44
# File 'lib/traject_plus/indexer/step.rb', line 42

def indexer
  @indexer
end

Instance Method Details

#execute(context) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/traject_plus/indexer/step.rb', line 52

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

  if procs
    procs.call(context.source_record, accumulator, context)
  else
    accumulator << context.source_record
  end

  accumulator.map do |record|
    new_context = Traject::Indexer::Context.new(source_record: record, settings: indexer.settings)
    new_context.clipboard[:parent] = context
    indexer.map_to_context!(new_context)
    result = new_context.output_hash

    if field_name
      self.class.add_accumulator_to_context! self, field_name, [result], context
    else
      result.each do |k, v|
        self.class.add_accumulator_to_context! self, k, Array(v), context
      end
    end
  end
end