Class: CSVPlusPlus::Language::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_plus_plus/language/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

Constructor Details

#initialize(runtime:, code_section: nil) ⇒ Scope

initialize with a Runtime and optional CodeSection



37
38
39
40
# File 'lib/csv_plus_plus/language/scope.rb', line 37

def initialize(runtime:, code_section: nil)
  @code_section = code_section if code_section
  @runtime = runtime
end

Instance Attribute Details

#code_sectionObject

Returns the value of attribute code_section.



34
35
36
# File 'lib/csv_plus_plus/language/scope.rb', line 34

def code_section
  @code_section
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



34
35
36
# File 'lib/csv_plus_plus/language/scope.rb', line 34

def runtime
  @runtime
end

Instance Method Details

#resolve_cell_valueObject

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/csv_plus_plus/language/scope.rb', line 43

def resolve_cell_value
  return unless (ast = @runtime.cell&.ast)

  last_round = nil
  loop do
    refs = ::CSVPlusPlus::Language::References.extract(ast, @code_section)
    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_sObject

to_s



67
68
69
# File 'lib/csv_plus_plus/language/scope.rb', line 67

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