Class: CSVPlusPlus::Scope

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:



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

#functionsObject (readonly)

TODO: split out a CanResolveReferences



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

def functions
  @functions
end

#runtimeObject (readonly)

TODO: split out a CanResolveReferences



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

def runtime
  @runtime
end

#variablesObject (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).

Parameters:

  • var_id (Symbol)

    The name of the variable to bind the cell reference to

Returns:

  • (CellReference)


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_valueEntity

Resolve all values in the ast of the current cell being processed

Returns:

  • (Entity)


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_sString

Returns:

  • (String)


62
63
64
# File 'lib/csv_plus_plus/scope.rb', line 62

def to_s
  "Scope(functions: #{@functions}, runtime: #{@runtime}, variables: #{@variables})"
end