Class: CSVPlusPlus::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_plus_plus/cell.rb

Overview

A cell of a template

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row_index:, index:, value:, modifier:) ⇒ Cell

Returns a new instance of Cell.

Parameters:

  • row_index (Integer)

    The cell’s row index (starts at 0)

  • index (Integer)

    The cell’s index (starts at 0)

  • value (String)

    A string value which should already have been processed through a CSV parser

  • modifier (Modifier)

    A modifier to apply to this cell



34
35
36
37
38
39
# File 'lib/csv_plus_plus/cell.rb', line 34

def initialize(row_index:, index:, value:, modifier:)
  @value = value
  @modifier = modifier
  @index = index
  @row_index = row_index
end

Instance Attribute Details

#astEntity

Returns the current value of ast.

Returns:

  • (Entity)

    the current value of ast



13
14
15
# File 'lib/csv_plus_plus/cell.rb', line 13

def ast
  @ast
end

#indexInteger (readonly)

The cell’s index (starts at 0)

Returns:

  • (Integer)

    the current value of index



13
14
15
# File 'lib/csv_plus_plus/cell.rb', line 13

def index
  @index
end

#modifierModifier (readonly)

The modifier for this cell

Returns:

  • (Modifier)

    the current value of modifier



13
14
15
# File 'lib/csv_plus_plus/cell.rb', line 13

def modifier
  @modifier
end

#row_indexInteger

The cell’s row index (starts at 0)

Returns:

  • (Integer)

    the current value of row_index



13
14
15
# File 'lib/csv_plus_plus/cell.rb', line 13

def row_index
  @row_index
end

Class Method Details

.parse(value, runtime:, modifier:) ⇒ Cell

Parse a value into a Cell object.

Parameters:

  • value (String)

    A string value which should already have been processed through a CSV parser

  • runtime (Runtime)
  • modifier (Modifier)

Returns:



24
25
26
27
28
# File 'lib/csv_plus_plus/cell.rb', line 24

def self.parse(value, runtime:, modifier:)
  new(value:, row_index: runtime.row_index, index: runtime.cell_index, modifier:).tap do |c|
    c.ast = ::CSVPlusPlus::Parser::CellValue.new.parse(value, runtime)
  end
end

Instance Method Details

#to_csvString

A compiled final representation of the cell. This can only happen after all cell have had variables and functions resolved.

Returns:

  • (String)


59
60
61
62
63
64
65
# File 'lib/csv_plus_plus/cell.rb', line 59

def to_csv
  return value unless @ast

  # This looks really simple but we're relying on each node of the AST to define #to_s such that calling
  # this at the top will recursively print the tree (as a well-formatted spreadsheet formula)
  "=#{@ast}"
end

#to_sString

Returns:

  • (String)


51
52
53
# File 'lib/csv_plus_plus/cell.rb', line 51

def to_s
  "Cell(index: #{@index}, row_index: #{@row_index}, value: #{@value}, modifier: #{@modifier})"
end

#valueString

The @value (cleaned up some)

Returns:

  • (String)


44
45
46
47
48
# File 'lib/csv_plus_plus/cell.rb', line 44

def value
  return if @value.nil? || @value.strip.empty?

  @value.strip
end