Sha256: f371950a5838af71ed7a30a3c86a7d6f919d3a825177ac352305b268c2c96b3d

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

require 'ripper'
require 'coypond/parser'

module Coypond
  class CoypondRunner
    
    def initialize(files)
      @files = files
    end
    
    def search(options)
      final_result = {}
      each_file do |file_name, source, parser|
        final_result[file_name] = []
        classes = modules = methods = {}
        classes = search_in_collection(parser.classes, options).to_a if options[:class] || options[:all]
        modules = search_in_collection(parser.modules, options).to_a if options[:module] || options[:all]
        methods = search_in_collection(parser.methods, options).to_a if options[:method] || options[:all]
        result = Hash[classes + modules + methods]
        result.each do |dec, location|
          context = source.lines.to_a[location.first - 1]
          final_result[file_name] << [dec, location, context]
        end
        
        yield(file_name, final_result[file_name]) if block_given?
        
      end
      return final_result
    end
    
    private
    def search_in_collection(collection, options)
      search_term = options[:search_term]
      search_term = search_term.downcase if options[:ignore_case]
      collection.select do |k,v|
        k = k.downcase if options[:ignore_case]
        if options[:regexp]
          Regexp.new("(#{search_term})$").match(k)
        else
          k.end_with?(search_term)
        end
      end
    end
    
    def each_file
      @files.each do |file|
        source = File.read(file)
        parse_tree = Ripper::SexpBuilder.new(source).parse
        parser = Coypond::Parser.new.parse(parse_tree)
        yield(file, source, parser)
      end
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
coypond-0.2.5 lib/coypond.rb
coypond-0.2.4 lib/coypond.rb
coypond-0.2.3 lib/coypond.rb