Class: CSVPlusPlus::Compiler
- Inherits:
-
Object
- Object
- CSVPlusPlus::Compiler
- 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
-
#options ⇒ Options
readonly
The
Optionsto compile with. -
#runtime ⇒ Runtime
readonly
The runtime execution.
-
#scope ⇒ Scope
readonly
Scopefor variable resolution.
Class Method Summary collapse
-
.with_compiler(runtime:, options:, &block) ⇒ Object
Create a compiler and make sure it gets cleaned up.
Instance Method Summary collapse
-
#compile_template ⇒ Template
Compile a template and return a
::CSVPlusPlus::Templateinstance ready to be written with aWriter. -
#initialize(runtime:, options:, scope: nil) ⇒ Compiler
constructor
A new instance of Compiler.
-
#outputting! ⇒ Object
Write the compiled results.
- #to_s ⇒ String
Constructor Details
#initialize(runtime:, options:, scope: nil) ⇒ Compiler
Returns a new instance of Compiler.
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 = @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( .key_values.transform_values { |v| ::CSVPlusPlus::Entities::String.new(v.to_s) } ) end |
Instance Attribute Details
#options ⇒ Options (readonly)
The Options to compile with
16 17 18 |
# File 'lib/csv_plus_plus/compiler.rb', line 16 def @options end |
#runtime ⇒ Runtime (readonly)
The runtime execution
16 17 18 |
# File 'lib/csv_plus_plus/compiler.rb', line 16 def runtime @runtime end |
#scope ⇒ Scope (readonly)
Scope for variable resolution
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
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 .verbose ::CSVPlusPlus::BenchmarkedCompiler.with_benchmarks(compiler) do |c| block.call(c) end else yield(compiler) end ensure runtime.cleanup! end |
Instance Method Details
#compile_template ⇒ Template
Compile a template and return a ::CSVPlusPlus::Template instance ready to be written with a Writer
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.(@runtime) { t. } 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_s ⇒ String
72 73 74 |
# File 'lib/csv_plus_plus/compiler.rb', line 72 def to_s "Compiler(options: #{@options}, runtime: #{@runtime}, scope: #{@scope})" end |