Class: Twig::Node::Base
- Inherits:
-
Object
- Object
- Twig::Node::Base
- Defined in:
- lib/twig/node/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
- #attributes ⇒ Hash readonly
- #lineno ⇒ Integer readonly
- #nodes ⇒ AutoHash{[Symbol, Integer] => Node::Base} readonly
- #source_context ⇒ Source
-
#tag ⇒ Object
Returns the value of attribute tag.
Instance Method Summary collapse
- #compile(compiler) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(nodes = {}, attributes = {}, lineno = 0) ⇒ Base
constructor
A new instance of Base.
- #length ⇒ Integer
- #template_name ⇒ String
- #to_s ⇒ Object
Constructor Details
#initialize(nodes = {}, attributes = {}, lineno = 0) ⇒ Base
Returns a new instance of Base.
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
#attributes ⇒ Hash (readonly)
14 15 16 |
# File 'lib/twig/node/base.rb', line 14 def attributes @attributes end |
#lineno ⇒ Integer (readonly)
11 12 13 |
# File 'lib/twig/node/base.rb', line 11 def lineno @lineno end |
#nodes ⇒ AutoHash{[Symbol, Integer] => Node::Base} (readonly)
20 21 22 |
# File 'lib/twig/node/base.rb', line 20 def nodes @nodes end |
#source_context ⇒ Source
17 18 19 |
# File 'lib/twig/node/base.rb', line 17 def source_context @source_context end |
#tag ⇒ Object
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
50 51 52 |
# File 'lib/twig/node/base.rb', line 50 def compile(compiler) @nodes.each_value { |node| compiler.subcompile(node) } end |
#empty? ⇒ Boolean
70 71 72 |
# File 'lib/twig/node/base.rb', line 70 def empty? nodes.empty? end |
#length ⇒ Integer
66 67 68 |
# File 'lib/twig/node/base.rb', line 66 def length nodes.length end |
#template_name ⇒ String
61 62 63 |
# File 'lib/twig/node/base.rb', line 61 def template_name source_context.name end |
#to_s ⇒ Object
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 |