Class: CSVPlusPlus::Cell

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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(index:, modifier:, row_index:, value:) ⇒ Cell

Returns a new instance of Cell.

Parameters:

  • index (Integer)

    The cell’s index (starts at 0)

  • modifier (Modifier)

    A modifier to apply to this cell

  • row_index (Integer)

    The cell’s row index (starts at 0)

  • value (String)

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



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

def initialize(index:, modifier:, row_index:, value:)
  @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



11
12
13
# File 'lib/csv_plus_plus/cell.rb', line 11

def ast
  @ast
end

#indexInteger (readonly)

The cell’s index (starts at 0)

Returns:

  • (Integer)

    the current value of index



11
12
13
# File 'lib/csv_plus_plus/cell.rb', line 11

def index
  @index
end

#modifierModifier (readonly)

The modifier for this cell

Returns:

  • (Modifier)

    the current value of modifier



11
12
13
# File 'lib/csv_plus_plus/cell.rb', line 11

def modifier
  @modifier
end

#row_indexInteger

The cell’s row index (starts at 0)

Returns:

  • (Integer)

    the current value of row_index



11
12
13
# File 'lib/csv_plus_plus/cell.rb', line 11

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:



40
41
42
43
44
# File 'lib/csv_plus_plus/cell.rb', line 40

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

Instance Method Details

#evaluate(runtime) ⇒ ::String

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

Parameters:

Returns:

  • (::String)


83
84
85
86
87
# File 'lib/csv_plus_plus/cell.rb', line 83

def evaluate(runtime)
  return value unless @ast

  "=#{@ast.evaluate(runtime)}"
end

#value::String

The @value (cleaned up some)

TODO: is this used?

Returns:

  • (::String)


70
71
72
73
74
# File 'lib/csv_plus_plus/cell.rb', line 70

def value
  stripped = @value&.strip

  stripped&.empty? ? nil : stripped
end