Class: CSVPlusPlus::Scope
- Inherits:
-
Object
- Object
- CSVPlusPlus::Scope
- Includes:
- CanDefineReferences
- Defined in:
- lib/csv_plus_plus/scope.rb
Overview
A class representing the scope of the current Template and responsible for resolving variables
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#functions ⇒ Object
readonly
TODO: split out a CanResolveReferences.
-
#runtime ⇒ Object
readonly
TODO: split out a CanResolveReferences.
-
#variables ⇒ Object
readonly
TODO: split out a CanResolveReferences.
Instance Method Summary collapse
-
#bind_variable_to_cell(var_id) ⇒ CellReference
Bind
var_idto the current cell (based on where @runtime is currently pointing). -
#initialize(runtime:, functions: {}, variables: {}) ⇒ Scope
constructor
A new instance of Scope.
-
#resolve_cell_value ⇒ Entity
Resolve all values in the ast of the current cell being processed.
- #to_s ⇒ String
Methods included from CanDefineReferences
#def_function, #def_variable, #def_variables, #defined_function?, #defined_variable?, #verbose_summary
Constructor Details
#initialize(runtime:, functions: {}, variables: {}) ⇒ Scope
Returns a new instance of Scope.
23 24 25 26 27 |
# File 'lib/csv_plus_plus/scope.rb', line 23 def initialize(runtime:, functions: {}, variables: {}) @runtime = runtime @functions = functions @variables = variables end |
Instance Attribute Details
#functions ⇒ Object (readonly)
TODO: split out a CanResolveReferences
16 17 18 |
# File 'lib/csv_plus_plus/scope.rb', line 16 def functions @functions end |
#runtime ⇒ Object (readonly)
TODO: split out a CanResolveReferences
16 17 18 |
# File 'lib/csv_plus_plus/scope.rb', line 16 def runtime @runtime end |
#variables ⇒ Object (readonly)
TODO: split out a CanResolveReferences
16 17 18 |
# File 'lib/csv_plus_plus/scope.rb', line 16 def variables @variables end |
Instance Method Details
#bind_variable_to_cell(var_id) ⇒ CellReference
Bind var_id to the current cell (based on where @runtime is currently pointing).
52 53 54 55 56 57 58 59 |
# File 'lib/csv_plus_plus/scope.rb', line 52 def bind_variable_to_cell(var_id) ::CSVPlusPlus::Entities::CellReference.from_index( cell_index: runtime.cell_index, row_index: runtime.row_index ).tap do |cell_reference| def_variable(var_id, cell_reference) end end |
#resolve_cell_value ⇒ Entity
Resolve all values in the ast of the current cell being processed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/csv_plus_plus/scope.rb', line 32 def resolve_cell_value return unless (ast = @runtime.cell&.ast) last_round = nil loop do refs = ::CSVPlusPlus::References.extract(ast, self) return ast if refs.empty? # TODO: throw an error here instead I think - basically we did a round and didn't make progress return ast if last_round == refs ast = resolve_functions(resolve_variables(ast, refs.variables), refs.functions) end end |
#to_s ⇒ String
62 63 64 |
# File 'lib/csv_plus_plus/scope.rb', line 62 def to_s "Scope(functions: #{@functions}, runtime: #{@runtime}, variables: #{@variables})" end |