Class: Bade::AST::StringSerializer
- Defined in:
- lib/bade/ast/string_serializer.rb
Instance Attribute Summary collapse
- #root ⇒ AST::Node, AST::Document readonly
Instance Method Summary collapse
-
#initialize(root) ⇒ StringSerializer
constructor
A new instance of StringSerializer.
- #node_to_s(node, level) ⇒ String
- #to_s ⇒ Object
Constructor Details
#initialize(root) ⇒ StringSerializer
Returns a new instance of StringSerializer.
13 14 15 |
# File 'lib/bade/ast/string_serializer.rb', line 13 def initialize(root) @root = root end |
Instance Attribute Details
#root ⇒ AST::Node, AST::Document (readonly)
9 10 11 |
# File 'lib/bade/ast/string_serializer.rb', line 9 def root @root end |
Instance Method Details
#node_to_s(node, level) ⇒ String
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/bade/ast/string_serializer.rb', line 34 def node_to_s(node, level) type_s = node.type.inspect indent = ' ' * level children_s = '' if node.children.count > 0 children_s = "\n#{node.children.map { |n| node_to_s(n, level + 1) }.join("\n")}\n#{indent}" end other = '' case node when TagNode, MixinCommonNode other = node.name when KeyValueNode other = "#{node.name}:#{node.value}" when ValueNode, StaticTextNode escaped_sign = if node.escaped '& ' elsif node.escaped.nil? '&(nil) ' else '' end other = "#{escaped_sign}#{node.value}" when Node # nothing else raise "Unknown node class #{node.class} of type #{node.type} for serializing" end other = " #{other}" if other && !other.empty? "#{indent}(#{type_s}#{other}#{children_s})" end |
#to_s ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/bade/ast/string_serializer.rb', line 17 def to_s case root when Node node_to_s(root, 0) when Document node_to_s(root.root, 0) else msg = "Root attribute passed into initializer must be subclass of #{Node} or #{Document}, is #{root.class}!" raise AttributeError, msg end end |