123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Support::Source::Node Private

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
RSpec::Support::Source::ExpressionSequenceNode
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable
Inherits: Object
Defined in: rspec-support/lib/rspec/support/source/node.rb

Overview

A wrapper for Ripper AST node which is generated with Ripper.sexp.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Class Method Details

.sexp?(array) ⇒ Boolean

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 15

def self.sexp?(array)
  array.is_a?(Array) && array.first.is_a?(Symbol)
end

Instance Attribute Details

#parent (readonly)

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 13

attr_reader :sexp, :parent

#sexp (readonly)

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 13

attr_reader :sexp, :parent

Instance Method Details

#args

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 28

def args
  @args ||= raw_args.map do |raw_arg|
    if Node.sexp?(raw_arg)
      Node.new(raw_arg, self)
    elsif Location.location?(raw_arg)
      Location.new(*raw_arg)
    elsif raw_arg.is_a?(Array)
      ExpressionSequenceNode.new(raw_arg, self)
    else
      raw_arg
    end
  end.freeze
end

#children

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 42

def children
  @children ||= args.select { |arg| arg.is_a?(Node) }.freeze
end

#each

We use a loop here (instead of recursion) to prevent SystemStackError

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 51

def each
  return to_enum(__method__) unless block_given?

  node_queue = []
  node_queue << self

  while (current_node = node_queue.shift)
    yield current_node
    node_queue.concat(current_node.children)
  end
end

#each_ancestor

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 63

def each_ancestor
  return to_enum(__method__) unless block_given?

  current_node = self

  while (current_node = current_node.parent)
    yield current_node
  end
end

#inspect

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 73

def inspect
  "#<#{self.class} #{type}>"
end

#location

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 46

def location
  @location ||= args.find { |arg| arg.is_a?(Location) }
end

#raw_args (private)

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 79

def raw_args
  sexp[1..-1] || []
end

#type

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source/node.rb', line 24

def type
  sexp[0]
end