class CallSearcher::Searcher
Public Class Methods
new(&blk)
click to toggle source
# File lib/call_searcher/searcher.rb, line 5 def initialize(&blk) @condition = blk || ->(*) { true } end
Public Instance Methods
search(file:, context: nil)
click to toggle source
# File lib/call_searcher/searcher.rb, line 19 def search(file:, context: nil) context = context&.dup || CallSearcher::Context.new context.path = file ast = RubyVM::AbstractSyntaxTree.parse_file(file) filter(ast, context) end
search_dir(root_dir: '.', path:, github: nil)
click to toggle source
# File lib/call_searcher/searcher.rb, line 9 def search_dir(root_dir: '.', path:, github: nil) path = File.expand_path(path, root_dir) context = CallSearcher::Context.new(root_dir: root_dir, github: github) Dir.glob(File.join(path, "**", "*.rb")).reduce([]) do |arr, f| if File.file?(f) arr + search(file: f, context: context) end end end
Private Instance Methods
filter(node, context)
click to toggle source
# File lib/call_searcher/searcher.rb, line 30 def filter(node, context) ret = node.children.grep(RubyVM::AbstractSyntaxTree::Node).flat_map do |child| filter(child, context) end case node.type when :CALL, :QCALL, :OPCALL, :FCALL, :VCALL method_call = CallSearcher::MethodCall.new(node: node, context: context) if @condition.call(method_call) ret << method_call end else nil end ret end