Class: CSVPlusPlus::Template

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

Overview

Contains the flow and data from a code section and CSV section

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows:, scope:) ⇒ Template

initialize



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

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

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



6
7
8
# File 'lib/csv_plus_plus/template.rb', line 6

def rows
  @rows
end

#scopeObject (readonly)

Returns the value of attribute scope.



6
7
8
# File 'lib/csv_plus_plus/template.rb', line 6

def scope
  @scope
end

Instance Method Details

#expand_rows!Object

Apply any expand= modifiers to the parsed template



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/csv_plus_plus/template.rb', line 20

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_sObject

to_s



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

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



35
36
37
38
39
40
41
42
43
# File 'lib/csv_plus_plus/template.rb', line 35

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

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