Sha256: cc7e0ddf438aeed6f7a55fd2bf0148d127fba2b1967534ba6553b83e8212151a

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'rubygems'
require 'sexp'

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
end

Version data entries

1 entries across 1 versions & 1 rubygems

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