Sha256: 615bbd501c7db9ee77faa5761685df793179b8eab92ce6f2a494e1bd15391a0b

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

require 'rubygems'
require 'sexp'
require 'ruby2ruby'

class Sexp
  def accept(visitor)
    visitor.visit(self)
  end

  def node_type
    first
  end

  def children
    find_all { | sexp | Sexp === sexp }
  end
  
  def is_language_node?
    first.class == Symbol
  end
  
  def visitable_children
    parent = is_language_node? ? sexp_body : self
    parent.children
  end
  
  def recursive_children(&handler)
    visitable_children.each do |child|
      handler.call child
      child.recursive_children(&handler)
    end
  end
  
  def grep_nodes(options)
    return self if options.empty?
    node_type = options[:node_type]
    subject = options[:subject]
    message = options[:message]
    arguments = options[:arguments]
    nodes = []
    self.recursive_children do |child|
      if (!node_type or node_type == child.node_type) and (!subject or subject == child.subject) and (!message or message == child.message) and (!arguments or arguments == child.arguments)
        nodes << child
      end
    end
    nodes
  end
  
  def subject
    case node_type
    when :attrasgn, :call, :iasgn, :lasgn
      self[1]
    else
    end
  end
  
  def message
    case node_type
    when :attrasgn, :call
      self[2]
    else
    end
  end
  
  def arguments
    case node_type
    when :attrasgn, :call
      self[3]
    else
    end
  end
  
  def call
    case node_type
    when :if, :arglist
      self[1]
    else
    end
  end
  
  def true_node
    case node_type
    when :if
      self[2]
    else
    end
  end
  
  def false_node
    case node_type
    when :if
      self[3]
    else
    end
  end
  
  def method_body
    case node_type
    when :block
      self[1..-1]
    else
    end
  end

  def to_ruby
    Ruby2Ruby.new.process(self)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_best_practices-0.1.2 lib/rails_best_practices/core/visitable_sexp.rb