Class: Twig::Node::Expression::Filter

Inherits:
Call show all
Defined in:
lib/twig/node/expression/filter.rb

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

#template_name

Constructor Details

#initialize(node, filter, arguments, lineno) ⇒ Filter

Returns a new instance of Filter.

Parameters:



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

def initialize(node, filter, arguments, lineno)
  if filter.is_a?(TwigFilter)
    name = filter.name
    filter_name = Constant.new(name, lineno)
  else
    name = filter.attributes[:value]
    filter_name = filter
  end

  super({
    node:,
    filter: filter_name,
    arguments:,
  }, {
    name:,
    type: 'filter',
  }, lineno)

  if filter.is_a?(Filter)
    attributes[:twig_callable] = filter
  end
end

Instance Method Details

#compile(compiler) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/twig/node/expression/filter.rb', line 32

def compile(compiler)
  name = nodes[:filter].attributes[:value]

  if name != attributes[:name]
    raise 'Changing the value of a "filter" node is not supported'
  end

  if name == 'raw'
    raise 'Cannot create raw filter via expression'
  end

  unless attributes.key?(:twig_callable)
    attributes[:twig_callable] = compiler.environment.filter(name)
  end

  compile_callable(compiler)
end