Sha256: 34c1ee0c8353fab7d7be53413730342f285d65358f5f815df3273677704e9f85

Contents?: true

Size: 1.98 KB

Versions: 19

Compression:

Stored size: 1.98 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
    if [:attrasgn, :call, :iasgn, :lasgn, :class].include? node_type
      self[1]
    end
  end
  
  def message
    if [:attrasgn, :call, :defs].include? node_type
      self[2]
    end
  end
  
  def arguments
    if [:attrasgn, :call].include? node_type
      self[3]
    end
  end
  
  def call
    if [:if, :arglist].include? node_type
      self[1]
    end
  end
  
  def true_node
    if :if == node_type
      self[2]
    end
  end
  
  def false_node
    if :if == node_type
      self[3]
    end
  end

  def message_name
    if :defn == node_type
      self[1]
    end
  end
  
  def body
    if :block == node_type
      self[1..-1]
    elsif :class == node_type
      self[3]
    elsif :defn == node_type
      self[3][1]
    elsif :defs == node_type
      self[4][1]
    end
  end

  def to_ruby
    Ruby2Ruby.new.process(self) unless self.empty?
  end
  
  def to_ruby_string
    return nil if self.empty?
    eval(Ruby2Ruby.new.process(self)).to_s
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
rails_best_practices-0.3.13 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.12 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.11 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.10 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.9 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.8 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.7 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.6 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.5 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.4 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.3 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.2 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.1 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.3.0 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.2.16 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.2.15 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.2.14 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.2.13 lib/rails_best_practices/core/visitable_sexp.rb
rails_best_practices-0.2.12 lib/rails_best_practices/core/visitable_sexp.rb