Class: CSVPlusPlus::Compiler

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

Overview

Encapsulates the parsing and building of objects (Template -> Row -> Cell). Variable resolution is delegated to the Scope

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime:, options:, scope: nil) ⇒ Compiler

Returns a new instance of Compiler.

Parameters:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/csv_plus_plus/compiler.rb', line 39

def initialize(runtime:, options:, scope: nil)
  @options = options
  @runtime = runtime
  @scope = scope || ::CSVPlusPlus::Scope.new(runtime:)

  # TODO: infer a type
  # allow user-supplied key/values to override anything global or from the code section
  @scope.def_variables(
    options.key_values.transform_values { |v| ::CSVPlusPlus::Entities::String.new(v.to_s) }
  )
end

Instance Attribute Details

#optionsOptions (readonly)

The Options to compile with

Returns:

  • (Options)

    the current value of options



16
17
18
# File 'lib/csv_plus_plus/compiler.rb', line 16

def options
  @options
end

#runtimeRuntime (readonly)

The runtime execution

Returns:

  • (Runtime)

    the current value of runtime



16
17
18
# File 'lib/csv_plus_plus/compiler.rb', line 16

def runtime
  @runtime
end

#scopeScope (readonly)

Scope for variable resolution

Returns:

  • (Scope)

    the current value of scope



16
17
18
# File 'lib/csv_plus_plus/compiler.rb', line 16

def scope
  @scope
end

Class Method Details

.with_compiler(runtime:, options:, &block) ⇒ Object

Create a compiler and make sure it gets cleaned up

Parameters:

  • runtime (Runtime)

    The initial Runtime for the compiler

  • options (Options)


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/csv_plus_plus/compiler.rb', line 23

def self.with_compiler(runtime:, options:, &block)
  compiler = new(options:, runtime:)
  if options.verbose
    ::CSVPlusPlus::BenchmarkedCompiler.with_benchmarks(compiler) do |c|
      block.call(c)
    end
  else
    yield(compiler)
  end
ensure
  runtime.cleanup!
end

Instance Method Details

#compile_templateTemplate

Compile a template and return a ::CSVPlusPlus::Template instance ready to be written with a Writer

Returns:



60
61
62
63
64
65
66
67
68
69
# File 'lib/csv_plus_plus/compiler.rb', line 60

def compile_template
  parse_code_section!
  rows = parse_csv_section!

  ::CSVPlusPlus::Template.new(rows:, scope: @scope).tap do |t|
    t.validate_infinite_expands(@runtime)
    expanding { t.expand_rows! }
    resolve_all_cells!(t)
  end
end

#outputting!Object

Write the compiled results



52
53
54
55
# File 'lib/csv_plus_plus/compiler.rb', line 52

def outputting!
  @runtime.start_at_csv!
  yield
end

#to_sString

Returns:

  • (String)


72
73
74
# File 'lib/csv_plus_plus/compiler.rb', line 72

def to_s
  "Compiler(options: #{@options}, runtime: #{@runtime}, scope: #{@scope})"
end