Module: CSVPlusPlus::Runtime::PositionTracker

Extended by:
T::Sig
Included in:
Runtime
Defined in:
lib/csv_plus_plus/runtime/position_tracker.rb

Overview

Functions needed to track all of the runtime pointers: current line number, current row number, current cell, etc. rubocop:disable Metrics/ModuleLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cellCell

The current cell index. This will only be set when processing the CSV section

Returns:



27
28
29
30
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 27

def cell
  @cell ||= ::T.let(nil, ::T.nilable(::CSVPlusPlus::Cell))
  assert_initted!(@cell)
end

#cell_indexInteger

The current CSV cell index.

This will only be set when processing the CSV section and will throw an exception otherwise. It is up to the caller (the compiler) to make sure it’s called in the context of a compilation stage and/or a #map_row/#map_rows/#map_lines

Returns:

  • (Integer)


40
41
42
43
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 40

def cell_index
  @cell_index ||= ::T.let(nil, ::T.nilable(::Integer))
  assert_initted!(@cell_index)
end

#line_numberInteger

The current line number being processed. The line number is based on the entire file, irregardless of if it’s parsing the code section or the CSV section

This will only be set when processing the csvpp file and will throw an exception otherwise. It is up to the caller (the compiler) to make sure it’s called in the context of a compilation stage and/or a #map_row/#map_rows/#map_lines

Returns:

  • (Integer)


67
68
69
70
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 67

def line_number
  @line_number ||= ::T.let(nil, ::T.nilable(::Integer))
  assert_initted!(@line_number)
end

#row_indexInteger

The current CSV row index. This will only be set when processing the CSV section

This will only be set when processing the CSV section and will throw an exception otherwise. It is up to the caller (the compiler) to make sure it’s called in the context of a compilation stage and/or a #map_row/#map_rows/#map_lines

Returns:

  • (Integer)


53
54
55
56
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 53

def row_index
  @row_index ||= ::T.let(nil, ::T.nilable(::Integer))
  assert_initted!(@row_index)
end

Instance Method Details

#cleanup!Object

Clean up the Tempfile we’re using for parsing



74
75
76
77
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 74

def cleanup!
  input&.close
  input&.unlink
end

#input::Tempfile

The currently available input for parsing. The tmp state will be re-written between parsing the code section and the CSV section

Returns:

  • (::Tempfile)


84
85
86
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 84

def input
  @input ||= ::T.let(::Tempfile.new, ::T.nilable(::Tempfile))
end

#map_all_cells(rows, &block) ⇒ Array<Array>

Map over all rows and over all of their cells, calling the &block with each Cell

rubocop:disable Naming/BlockForwarding

Parameters:

  • rows (Array<Row>)

Returns:

  • (Array<Array>)


162
163
164
165
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 162

def map_all_cells(rows, &block)
  self.row_index = 0
  map_lines(rows) { |row| map_row(row.cells, &block) }
end

#map_lines(lines, &block) ⇒ Array

Map over a csvpp file and keep track of line_number and row_index

Parameters:

  • lines (Array)

Returns:

  • (Array)


99
100
101
102
103
104
105
106
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 99

def map_lines(lines, &block)
  self.line_number = 1
  lines.map do |line|
    ret = block.call(line)
    next_line!
    ret
  end
end

#map_row(row, &block) ⇒ Array

Map over a single row and keep track of the cell and it’s index

Parameters:

  • row (Array<Cell>)

    The row to map each cell over

Returns:

  • (Array)


124
125
126
127
128
129
130
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 124

def map_row(row, &block)
  row.map.with_index do |cell, index|
    self.cell_index = index
    self.cell = cell if cell.is_a?(::CSVPlusPlus::Cell)
    block.call(cell, index)
  end
end

#map_rows(rows, &block) ⇒ Array

Map over all rows and keep track of row and line numbers

Parameters:

  • rows (Array<Row>)

    The rows to map over (and keep track of indexes)

Returns:

  • (Array)


143
144
145
146
147
148
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 143

def map_rows(rows, &block)
  self.row_index = 0
  map_lines(rows) do |row|
    block.call(row)
  end
end

#rewrite_input!(data) ⇒ Object

We mutate the input over and over. It’s ok because it’s just a Tempfile

Parameters:

  • data (::String)

    The data to rewrite our input file to



193
194
195
196
197
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 193

def rewrite_input!(data)
  input&.truncate(0)
  input&.write(data)
  input&.rewind
end

#rownumInteger?

Return the current spreadsheet row number. It parallels @row_index but starts at 1.

Returns:

  • (Integer, nil)


172
173
174
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 172

def rownum
  row_index + 1
end

#start!(&block) ⇒ Object

Each time we run a parse on the input, reset the runtime state starting at the beginning of the file



180
181
182
183
184
185
186
187
# File 'lib/csv_plus_plus/runtime/position_tracker.rb', line 180

def start!(&block)
  @row_index = @cell_index = 0
  self.line_number = 1

  ret = block.call
  finish!
  ret
end