Class: Twig::Node::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/twig/node/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes = {}, attributes = {}, lineno = 0) ⇒ Base

Returns a new instance of Base.

Parameters:

  • nodes (Hash<Node::Base>) (defaults to: {})
  • attributes (Hash) (defaults to: {})
  • lineno (Integer) (defaults to: 0)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/twig/node/base.rb', line 25

def initialize(nodes = {}, attributes = {}, lineno = 0)
  invalid = nodes.
    values.
    detect { |node| !node.class.ancestors.include?(Node::Base) }

  raise "#{invalid.inspect} does not extend from #{Node::Base.name}" if invalid

  @nodes = AutoHash[nodes]
  @nodes.default_proc = ->(_hash, key) { raise "Node '#{key}' does not exist" }

  @attributes = attributes
  @attributes.default_proc = ->(_hash, key) { raise "Attribute '#{key}' does not exist" }

  @lineno = lineno
  @tag = nil
end

Instance Attribute Details

#attributesHash (readonly)

Returns:

  • (Hash)


14
15
16
# File 'lib/twig/node/base.rb', line 14

def attributes
  @attributes
end

#linenoInteger (readonly)

Returns:

  • (Integer)


11
12
13
# File 'lib/twig/node/base.rb', line 11

def lineno
  @lineno
end

#nodesAutoHash{[Symbol, Integer] => Node::Base} (readonly)

Returns:



20
21
22
# File 'lib/twig/node/base.rb', line 20

def nodes
  @nodes
end

#source_contextSource

Returns:



17
18
19
# File 'lib/twig/node/base.rb', line 17

def source_context
  @source_context
end

#tagObject

Returns the value of attribute tag.



8
9
10
# File 'lib/twig/node/base.rb', line 8

def tag
  @tag
end

Instance Method Details

#compile(compiler) ⇒ Object

Parameters:



50
51
52
# File 'lib/twig/node/base.rb', line 50

def compile(compiler)
  @nodes.each_value { |node| compiler.subcompile(node) }
end

#empty?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/twig/node/base.rb', line 70

def empty?
  nodes.empty?
end

#lengthInteger

Returns:

  • (Integer)


66
67
68
# File 'lib/twig/node/base.rb', line 66

def length
  nodes.length
end

#template_nameString

Returns:

  • (String)


61
62
63
# File 'lib/twig/node/base.rb', line 61

def template_name
  source_context.name
end

#to_sObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/twig/node/base.rb', line 74

def to_s
  repr = +''
  repr << self.class.name

  if @tag
    repr << "\n tag: #{@tag}"
  end

  attr = attributes.map do |name, value|
    v = if value.is_a?(Proc) || value.is_a?(Method)
          '\Closure'
        elsif value.is_a?(String)
          value
        else
          value.inspect
        end

    "#{name}: #{v}"
  end

  unless attr.empty?
    repr << "\n  attributes:\n    #{attr.join("\n    ")}"
  end

  unless empty?
    repr << "\n  nodes:"

    nodes.each do |name, node|
      len = name.to_s.length + 6
      node_repr = []

      node.to_s.each_line do |line|
        node_repr << ((' ' * len) + line.rstrip)
      end

      repr << "\n    #{name}: #{node_repr.join("\n").lstrip}"
    end
  end

  repr
end