Class: CSVPlusPlus::Template

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

Overview

Contains the data from a parsed csvpp template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows:, scope:) ⇒ Template

Returns a new instance of Template.

Parameters:

  • rows (Array<Row>)

    The +Row+s that comprise this Template

  • scope (Scope)

    The Scope containing all function and variable references



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

def initialize(rows:, scope:)
  @scope = scope
  @rows = rows
end

Instance Attribute Details

#rowsArray<Row> (readonly)

The +Row+s that comprise this Template

Returns:

  • (Array<Row>)

    the current value of rows



8
9
10
# File 'lib/csv_plus_plus/template.rb', line 8

def rows
  @rows
end

#scopeScope (readonly)

The Scope containing all function and variable references

Returns:

  • (Scope)

    the current value of scope



8
9
10
# File 'lib/csv_plus_plus/template.rb', line 8

def scope
  @scope
end

Instance Method Details

#expand_rows!Array<Row>

Apply any expand= modifiers to the parsed template

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/csv_plus_plus/template.rb', line 26

def expand_rows!
  expanded_rows = []
  row_index = 0
  expand_rows(
    lambda do |new_row|
      new_row.index = row_index
      expanded_rows << new_row
      row_index += 1
    end
  )

  @rows = expanded_rows
end

#to_sString

Returns:

  • (String)


19
20
21
# File 'lib/csv_plus_plus/template.rb', line 19

def to_s
  "Template(rows: #{@rows}, scope: #{@scope})"
end

#validate_infinite_expands(runtime) ⇒ Object

Make sure that the template has a valid amount of infinite expand modifiers

Parameters:

  • runtime (Runtime)

    The compiler's current runtime



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

def validate_infinite_expands(runtime)
  infinite_expand_rows = @rows.filter { |r| r.modifier.expand&.infinite? }
  return unless infinite_expand_rows.length > 1

  runtime.raise_formula_syntax_error(
    'You can only have one infinite expand= (on all others you must specify an amount)',
    infinite_expand_rows[1]
  )
end

#verbose_summaryString

Provide a summary of the state of the template (and it's @scope)

Returns:

  • (String)


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

def verbose_summary
  # TODO: we can probably include way more stats in here
  "    \#{@scope.verbose_summary}\n\n    > \#{@rows.length} rows to be written\n  SUMMARY\nend\n"