class Oga::Ruby::Node
Class representing a single node in a Ruby
AST.
The setup of this class is roughly based on the “ast” Gem. The “ast” Gem is not used for this class as it provides too many methods that might conflict with this class’ {#method_missing}.
ASTs can be built by creating a node and then chaining various method calls together. For example, the following could be used to build an “if” statement:
number1 = Node.new(:lit, %w{10}) number2 = Node.new(:lit, %w{20}) (number2 > number1).if_true do Node.new(:lit, %w{30}) end
When serialized to Ruby
this would roughly lead to the following code:
if 20 > 10 30 end
@private
Attributes
@return [Symbol]
Public Class Methods
@param [Symbol] type @param [Array] children
# File lib/oga/ruby/node.rb, line 35 def initialize(type, children = []) @type = type.to_sym @children = children end
Public Instance Methods
Wraps the current node in a block.
@param [Array] args Arguments (as Node
instances) to pass to the block. @return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 114 def add_block(*args) Node.new(:block, [self, args, yield]) end
Returns a boolean “and” node.
@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 81 def and(other) Node.new(:and, [self, other]) end
Returns an assignment node.
This method wraps assigned values in a begin/end block to ensure that multiple lines of code result in the proper value being assigned.
@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 61 def assign(other) if other.type == :followed_by other = other.wrap end Node.new(:assign, [self, other]) end
Adds an “else” statement to the current node.
This method assumes it’s being called only on “if” nodes.
@return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 157 def else Node.new(:if, @children + [yield]) end
Returns an equality expression node.
@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 73 def eq(other) Node.new(:eq, [self, other]) end
Chains two nodes together.
@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 165 def followed_by(other = nil) other = yield if ::Kernel.block_given? Node.new(:followed_by, [self, other]) end
Wraps the current node in an ‘if !…` statement.
@see [#if_true]
# File lib/oga/ruby/node.rb, line 138 def if_false self.not.if_true { yield } end
Wraps the current node in an if statement node.
The body of this statement is set to the return value of the supplied block.
@return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 131 def if_true Node.new(:if, [self, yield]) end
@return [String]
# File lib/oga/ruby/node.rb, line 184 def inspect "(#{type} #{@children.map(&:inspect).join(' ')})" end
Returns a node for Ruby’s “is_a?” method.
@param [Class] klass @return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 106 def is_a?(klass) Node.new(:send, [self, 'is_a?', Node.new(:lit, [klass.to_s])]) end
Returns a node for a method call.
@param [Symbol] name The name of the method to call.
@param [Array] args Any arguments (as Node
instances) to pass to the
method.
@return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 179 def method_missing(name, *args) Node.new(:send, [self, name.to_s, *args]) end
Returns a node that evaluates to its inverse.
For example, a variable ‘foo` would be turned into `!foo`.
@return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 98 def not !self end
Returns a boolean “or” node.
@param [Oga::Ruby::Node] other @return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 89 def or(other) Node.new(:or, [self, other]) end
@return [Array]
# File lib/oga/ruby/node.rb, line 41 def to_a @children end
Returns a “to_a” call node.
@return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 50 def to_array Node.new(:send, [self, :to_a]) end
Wraps the current node in a ‘while` statement.
The body of this statement is set to the return value of the supplied block.
@return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 148 def while_true Node.new(:while, [self, yield]) end
Wraps the current node in a ‘begin` node.
@return [Oga::Ruby::Node]
# File lib/oga/ruby/node.rb, line 121 def wrap Node.new(:begin, [self]) end