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

initialize



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

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

Instance Attribute Details

#astObject

Returns the value of attribute ast.



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

def ast
  @ast
end

#indexObject (readonly)

Returns the value of attribute index.



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

def index
  @index
end

#modifierObject (readonly)

Returns the value of attribute modifier.



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

def modifier
  @modifier
end

#row_indexObject

Returns the value of attribute row_index.



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

def row_index
  @row_index
end

Class Method Details

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

Parse a value into a Cell object. The value should already have been through a CSV parser



15
16
17
18
19
# File 'lib/csv_plus_plus/cell.rb', line 15

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

Instance Method Details

#to_csvObject

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



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

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 and calling
  # this at the top will recursively print the tree
  "=#{@ast}"
end

#to_sObject

to_s



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

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

#valueObject

The value (cleaned up some)



30
31
32
33
34
# File 'lib/csv_plus_plus/cell.rb', line 30

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

  @value.strip
end