Class: CSVPlusPlus::Compiler
- Inherits:
-
Object
- Object
- CSVPlusPlus::Compiler
- Extended by:
- T::Sig
- 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
rubocop:disable Metrics/ClassLength
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Options
readonly
The
Options
to compile with. -
#runtime ⇒ Runtime
readonly
The runtime execution.
Class Method Summary collapse
-
.with_compiler(options:, runtime:, &block) ⇒ Object
Create a compiler and make sure it gets cleaned up.
Instance Method Summary collapse
-
#benchmark=(benchmark) ⇒ Object
Attach a
Benchmark
and a place to store timings to the compiler class. -
#compile_template ⇒ Template
Compile a template and return a
::CSVPlusPlus::Template
instance ready to be written with aWriter
. -
#initialize(options:, runtime:) ⇒ Compiler
constructor
A new instance of Compiler.
-
#outputting!(&block) ⇒ Object
Write the compiled results.
Constructor Details
#initialize(options:, runtime:) ⇒ Compiler
Returns a new instance of Compiler.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/csv_plus_plus/compiler.rb', line 46 def initialize(options:, runtime:) @options = @runtime = runtime # TODO: infer a type # allow user-supplied key/values to override anything global or from the code section @runtime.scope.def_variables( .key_values.transform_values { |v| ::CSVPlusPlus::Entities::String.new(v.to_s) } ) end |
Instance Attribute Details
#options ⇒ Options (readonly)
The Options
to compile with
11 12 13 |
# File 'lib/csv_plus_plus/compiler.rb', line 11 def @options end |
#runtime ⇒ Runtime (readonly)
The runtime execution
11 12 13 |
# File 'lib/csv_plus_plus/compiler.rb', line 11 def runtime @runtime end |
Class Method Details
.with_compiler(options:, runtime:, &block) ⇒ Object
Create a compiler and make sure it gets cleaned up
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/csv_plus_plus/compiler.rb', line 31 def self.with_compiler(options:, runtime:, &block) if .verbose ::CSVPlusPlus::BenchmarkedCompiler.with_benchmarks(options:, runtime:) do |c| block.call(c) end else block.call(new(options:, runtime:)) end ensure runtime.position.cleanup! end |
Instance Method Details
#benchmark=(benchmark) ⇒ Object
Attach a Benchmark
and a place to store timings to the compiler class.
61 62 63 64 |
# File 'lib/csv_plus_plus/compiler.rb', line 61 def benchmark=(benchmark) @benchmark = ::T.let(benchmark, ::T.nilable(::Benchmark::Report)) @timings = ::T.let([], ::T.nilable(::T::Array[::Benchmark::Tms])) end |
#compile_template ⇒ Template
Compile a template and return a ::CSVPlusPlus::Template
instance ready to be written with a Writer
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/csv_plus_plus/compiler.rb', line 70 def compile_template parse_code_section! rows = parse_csv_section! ::CSVPlusPlus::Template.new(rows:, runtime: @runtime).tap do |t| t. { t. } bind_all_vars! { t.bind_all_vars!(@runtime) } resolve_all_cells!(t) end end |
#outputting!(&block) ⇒ Object
Write the compiled results
84 85 86 |
# File 'lib/csv_plus_plus/compiler.rb', line 84 def outputting!(&block) @runtime.start_at_csv! { block.call(@runtime.position) } end |