class Rbr::CLI

Public Class Methods

new() click to toggle source
# File lib/rbr/cli.rb, line 9
def initialize
  check_arg_count

  @matcher = ARGV[0].to_sym
  @condition = ARGV[1]
end

Public Instance Methods

check_arg_count() click to toggle source
# File lib/rbr/cli.rb, line 52
    def check_arg_count
      return true if ARGV.count >= 2

      warn <<~USAGE
        Usage:
          rbr MATCHER PATTERN FILE

        Examples:
          # find assignments to lvalue :@author in the file test/book.rb
          rbr assignment @author test/book.rb

          # find strings matching /Author.*name/ in the file test/book.rb
          rbr str "Author.*name" test/book.rb
      USAGE

      false
    end
expand_path(path) click to toggle source
# File lib/rbr/cli.rb, line 45
def expand_path(path)
  return [path] if File.file?(path)

  # if path is not a file, then glob all .rb files beneath it
  Dir.glob("#{path}/**/*.rb")
end
filenames() click to toggle source
# File lib/rbr/cli.rb, line 33
def filenames
  pattern = if ARGV.count > 2
              ARGV[2..]
            else
              ["."]
            end

  pattern.map { |arg| expand_path(arg) }
         .flatten
         .select { |filename| File.file?(filename) }
end
matching_nodes() click to toggle source
# File lib/rbr/cli.rb, line 22
def matching_nodes
  filenames.to_h do |filename|
    root, comments = Parser::CurrentRuby.parse_file_with_comments(filename)

    [filename, Query.new(@matcher, @condition).run(root, comments)]
  rescue EncodingError, Parser::SyntaxError => e
    warn "# Error parsing #{filename}: #{e}"
    [filename, []]
  end
end
start() click to toggle source
# File lib/rbr/cli.rb, line 16
def start
  matching_nodes.each do |filename, nodes|
    nodes.each { |node| puts "#{filename}:#{node.pretty_print}" }
  end
end