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.



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

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.



44
45
46
# File 'lib/traject_plus/indexer/step.rb', line 44

def indexer
  @indexer
end

Instance Method Details

#execute(context) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/traject_plus/indexer/step.rb', line 54

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.each_with_index.map do |record, index|
    new_context = Traject::Indexer::Context.new(
      source_record: record,
      settings: indexer.settings,
      position: index,
      position_in_input: index
    )
    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