Class: Twig::Node::Expression::Binary::NullCoalesce

Inherits:
Base show all
Includes:
OperatorEscape
Defined in:
lib/twig/node/expression/binary/null_coalesce.rb

Instance Attribute Summary

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Base

#operator

Methods inherited from Twig::Node::Expression::Base

#explicit_parentheses?, #set_explicit_parentheses

Methods inherited from Base

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

Constructor Details

#initialize(left, right, lineno) ⇒ NullCoalesce

Returns a new instance of NullCoalesce.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/twig/node/expression/binary/null_coalesce.rb', line 10

def initialize(left, right, lineno)
  super

  test = Test::Defined.new(left.dup, TwigTest.new('defined'), Node::Empty.new, left.lineno)

  unless left.is_a?(Expression::BlockReference)
    test = Binary::And.new(
      test,
      Unary::Not.new(
        Test::Null.new(left, TwigTest.new('null'), Node::Empty.new, left.lineno),
        left.lineno
      ),
      left.lineno
    )
  end

  left.attributes[:always_defined] = true
  nodes[:test] = test
end

Instance Method Details

#compile(compiler) ⇒ Object

Parameters:



31
32
33
34
35
36
37
38
39
40
# File 'lib/twig/node/expression/binary/null_coalesce.rb', line 31

def compile(compiler)
  compiler.
    raw('((').
    subcompile(nodes[:test]).
    raw(') ? (').
    subcompile(nodes[:left]).
    raw(') : (').
    subcompile(nodes[:right]).
    raw('))')
end

#operand_names_to_escapeObject



42
43
44
# File 'lib/twig/node/expression/binary/null_coalesce.rb', line 42

def operand_names_to_escape
  i[left right]
end