Class: CSVPlusPlus::CodeSection

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_plus_plus/code_section.rb

Overview

A representation of the code section part of a template (the variable and function definitions)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables: {}, functions: {}) ⇒ CodeSection

Returns a new instance of CodeSection.

Parameters:

  • variables (Hash<Symbol, Variable>) (defaults to: {})

    Initial variables

  • functions (Hash<Symbol, Variable>) (defaults to: {})

    Initial functions



17
18
19
20
# File 'lib/csv_plus_plus/code_section.rb', line 17

def initialize(variables: {}, functions: {})
  @variables = variables
  @functions = functions
end

Instance Attribute Details

#functionsHash<Symbol, Function> (readonly)

All defined functions

Returns:

  • (Hash<Symbol, Function>)

    the current value of functions



11
12
13
# File 'lib/csv_plus_plus/code_section.rb', line 11

def functions
  @functions
end

#variablesHash<Symbol, Variable>

All defined variables

Returns:

  • (Hash<Symbol, Variable>)

    the current value of variables



11
12
13
# File 'lib/csv_plus_plus/code_section.rb', line 11

def variables
  @variables
end

Instance Method Details

#def_function(id, entity) ⇒ Object

Define a (or re-define an existing) function

Parameters:

  • id (String, Symbol)

    The identifier for the function

  • entity (Entities::Function)

    The defined function



41
42
43
# File 'lib/csv_plus_plus/code_section.rb', line 41

def def_function(id, entity)
  @functions[id.to_sym] = entity
end

#def_variable(id, entity) ⇒ Object

Define a (or re-define an existing) variable

Parameters:

  • id (String, Symbol)

    The identifier for the variable

  • entity (Entity)

    The value (entity) the variable holds



26
27
28
# File 'lib/csv_plus_plus/code_section.rb', line 26

def def_variable(id, entity)
  @variables[id.to_sym] = entity
end

#def_variables(variables) ⇒ Object

Define (or re-define existing) variables

Parameters:

  • variables (Hash<Symbol, Variable>)

    Variables to define



33
34
35
# File 'lib/csv_plus_plus/code_section.rb', line 33

def def_variables(variables)
  variables.each { |id, entity| def_variable(id, entity) }
end

#defined_function?(fn_id) ⇒ boolean

Is the function defined?

Parameters:

  • fn_id (Symbol, String)

    The identifier of the function

Returns:

  • (boolean)


59
60
61
# File 'lib/csv_plus_plus/code_section.rb', line 59

def defined_function?(fn_id)
  @functions.key?(fn_id.to_sym)
end

#defined_variable?(var_id) ⇒ boolean

Is the variable defined?

Parameters:

  • var_id (Symbol, String)

    The identifier of the variable

Returns:

  • (boolean)


50
51
52
# File 'lib/csv_plus_plus/code_section.rb', line 50

def defined_variable?(var_id)
  @variables.key?(var_id.to_sym)
end

#to_sString

Returns:

  • (String)


64
65
66
# File 'lib/csv_plus_plus/code_section.rb', line 64

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

#verbose_summaryString

Provide a summary of the functions and variables compiled (to show in verbose mode)

Returns:

  • (String)


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/csv_plus_plus/code_section.rb', line 71

def verbose_summary
  <<~SUMMARY
    # Code Section Summary

    ## Resolved Variables

    #{variable_summary}

    ## Functions

    #{function_summary}
  SUMMARY
end