Class: Reading::Parsing::Attributes::Experiences::DatesAndHeadTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/reading/parsing/attributes/experiences/dates_and_head_transformer.rb

Overview

Experiences#transform_from_parsed delegates to this class when the History column is blank (i.e. when experiences should be extracted from the Start Dates, End Dates, and Head columns).

Instance Method Summary collapse

Constructor Details

#initialize(parsed_row, head_index) ⇒ DatesAndHeadTransformer

Returns a new instance of DatesAndHeadTransformer.

Parameters:

  • parsed_row (Hash)

    a parsed row (the intermediate hash).

  • head_index (Integer)

    current item’s position in the Head column.



17
18
19
20
# File 'lib/reading/parsing/attributes/experiences/dates_and_head_transformer.rb', line 17

def initialize(parsed_row, head_index)
  @parsed_row = parsed_row
  @head_index = head_index
end

Instance Method Details

#transformArray<Hash>

Extracts experiences from the parsed row.

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/reading/parsing/attributes/experiences/dates_and_head_transformer.rb', line 25

def transform
  size = [parsed_row[:start_dates]&.count || 0, parsed_row[:end_dates]&.count || 0].max
  # Pad start dates with {} and end dates with nil up to the size of
  # the larger of the two.
  start_dates = Array.new(size) { |i| parsed_row[:start_dates]&.dig(i) || {} }
  end_dates = Array.new(size) { |i| parsed_row[:end_dates]&.dig(i) || nil }

  start_end_dates = start_dates
    .reject { _1[:planned] }
    .zip(end_dates)
    .presence || [[{}, nil]]

  experiences_with_dates = start_end_dates.map { |start_entry, end_entry|
    {
      spans: spans(start_entry, end_entry),
      group: start_entry[:group],
      variant_index: (start_entry[:variant] || 1).to_i - 1,
    }.map { |k, v| [k, v || template.fetch(k)] }.to_h
  }.presence

  if experiences_with_dates
    Experiences::SpansValidator.validate(experiences_with_dates)
  end

  experiences_with_dates
end