Class: Twig::Node::Expression::Variable::Local

Inherits:
Base
  • Object
show all
Defined in:
lib/twig/node/expression/variable/local.rb

Constant Summary collapse

RESERVED_NAMES =
%w[varargs context macros blocks self].freeze

Instance Attribute Summary

Attributes inherited from Base

#attributes, #lineno, #nodes, #source_context, #tag

Instance Method Summary collapse

Methods inherited from Base

#explicit_parentheses?, #set_explicit_parentheses

Methods inherited from Base

#empty?, #length, #template_name, #to_s

Constructor Details

#initialize(name, lineno) ⇒ Local

Returns a new instance of Local.

Parameters:

  • (String|Integer|nil)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/twig/node/expression/variable/local.rb', line 11

def initialize(name, lineno)
  if %w[true false none null nil].include?(name.to_s.downcase)
    raise Error::Syntax.new("You cannot assign a value to \"#{name}\".", lineno)
  end

  # Convert to integer if name is an integer or consists of digits only
  if !name.nil? && (name.is_a?(Integer) || name == name.to_i.to_s)
    name = name.to_i
  elsif RESERVED_NAMES.include?(name)
    name = "\u035C#{name}"
  end

  super({}, { name: }, lineno)
end

Instance Method Details

#compile(compiler) ⇒ Object



26
27
28
29
30
# File 'lib/twig/node/expression/variable/local.rb', line 26

def compile(compiler)
  attributes[:name] = compiler.var_name if attributes[:name].nil?

  compiler.raw(attributes[:name].to_s)
end