Class: ActionviewPrecompiler::SyntaxTreeASTParser::RenderNode

Inherits:
Object
  • Object
show all
Defined in:
lib/actionview_precompiler/ast_parser/syntax_tree.rb

Overview

This class represents a node in the tree that is returned by the parser that corresponds to an argument to a render call, or a child of one of those nodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ RenderNode

Returns a new instance of RenderNode.



28
29
30
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 28

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



26
27
28
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 26

def node
  @node
end

Instance Method Details

#call?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 32

def call?
  node.is_a?(SyntaxTree::Call)
end

#hash?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 36

def hash?
  node.is_a?(SyntaxTree::HashLiteral) || node.is_a?(SyntaxTree::BareAssocHash)
end

#string?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 40

def string?
  node.is_a?(SyntaxTree::StringLiteral)
end

#symbol?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 44

def symbol?
  node.is_a?(SyntaxTree::Label) || node.is_a?(SyntaxTree::SymbolLiteral)
end

#to_hashObject

Converts the node into a hash where the keys and values are nodes. This will raise an error if the hash doesn’t match the format we expect or if the hash contains any splats.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 59

def to_hash
  case node
  in SyntaxTree::HashLiteral[assocs:]
  in SyntaxTree::BareAssocHash[assocs:]
  else
    raise CompilationError, "Unexpected node type: #{node.class.name}"
  end

  assocs.to_h do |assoc|
    case assoc
    in SyntaxTree::Assoc[key:, value:]
      [RenderNode.new(key), RenderNode.new(value)]
    else
      raise CompilationError, "Unexpected node type: #{node.class.name}"
    end
  end
end

#to_stringObject

Converts the node into a string value. Only handles plain string content, and will raise an error if the node contains interpolation.



79
80
81
82
83
84
85
86
87
88
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 79

def to_string
  case node
  in SyntaxTree::StringLiteral[parts: [SyntaxTree::TStringContent[value:]]]
    value
  in SyntaxTree::StringLiteral[parts:]
    raise CompilationError, "Unexpected string parts type: #{parts.inspect}"
  else
    raise CompilationError, "Unexpected node type: #{node.class.name}"
  end
end

#to_symbolObject

Converts the node into a symbol value. Only handles labels and plain symbols, and will raise an error if the node contains interpolation.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 92

def to_symbol
  case node
  in SyntaxTree::Label[value:]
    value.chomp(":").to_sym
  in SyntaxTree::SymbolLiteral[value: SyntaxTree::Ident[value:]]
    value.to_sym
  in SyntaxTree::SymbolLiteral[value:]
    raise CompilationError, "Unexpected symbol value type: #{value.inspect}"
  else
    raise CompilationError, "Unexpected node type: #{node.class.name}"
  end
end

#variable_reference?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 48

def variable_reference?
  node.is_a?(SyntaxTree::VarRef)
end

#vcall?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/actionview_precompiler/ast_parser/syntax_tree.rb', line 52

def vcall?
  node.is_a?(SyntaxTree::VCall)
end