Class: Equalshares::Pabulib::ProjectRowParser

Inherits:
Object
  • Object
show all
Defined in:
lib/equalshares/pabulib/project_row_parser.rb

Overview

Parses a single row of the PROJECTS section into the accumulator.

Instance Method Summary collapse

Constructor Details

#initialize(accumulator) ⇒ ProjectRowParser

Returns a new instance of ProjectRowParser.



7
8
9
# File 'lib/equalshares/pabulib/project_row_parser.rb', line 7

def initialize(accumulator)
  @acc = accumulator
end

Instance Method Details

#parse(row, header, line_number) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/equalshares/pabulib/project_row_parser.rb', line 11

def parse(row, header, line_number)
  project_id_idx = header.index("project_id")
  cost_idx = header.index("cost")
  name_idx = header.index("name")

  require_columns!(project_id_idx, cost_idx, line_number)
  # Validate the column count before any indexed access, so a short row raises a
  # ParseError rather than a NoMethodError on nil.
  validate_column_count!(row, header, line_number)

  project_id = row[project_id_idx].strip
  if @acc.project_ids_set[project_id]
    raise ParseError, "Line #{line_number}: Duplicate project ID '#{project_id}' found."
  end
  if row[project_id_idx].to_s.empty? || !Csv.numeric_string?(row[cost_idx])
    raise ParseError, "Line #{line_number}: Invalid or missing values in projects section."
  end

  store(row, header, project_id, name_idx)
end