class RubySearch::CLI

Attributes

options[R]

Public Class Methods

new() click to toggle source
# File lib/ruby_search/cli.rb, line 17
def initialize
  @options = parse_options
end
run() click to toggle source
# File lib/ruby_search/cli.rb, line 6
def self.run
  cli = new
  cli.load_environment
  results = Search.new.grep(cli.options[:q])
  results.each do |file, matches|
    Formatter.new(file, matches, cli.options).print
  end
end

Public Instance Methods

bundler_groups() click to toggle source
# File lib/ruby_search/cli.rb, line 52
def bundler_groups
  groups = [:default]
  groups << (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development').to_sym
end
load_environment() click to toggle source
# File lib/ruby_search/cli.rb, line 57
def load_environment
  # Set up gems listed in the Gemfile.
  ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', working_dir)
  Bundler.require(*bundler_groups) if File.exists?(ENV['BUNDLE_GEMFILE'])
end
parse_options(args=ARGV) click to toggle source
# File lib/ruby_search/cli.rb, line 21
def parse_options(args=ARGV)
  options = {}
  opt_parser = OptionParser.new do |opts|
    opts.banner = 'Usage: rs some_method [options]'
    opts.on '-e', '--escape', 'search literal string' do |e|
      options[:escape] = e
    end
    opts.on '-v', '--[no-]verbose', 'verbose output' do |v|
      options[:verbose] = v
    end
    opts.on '-c', '--[no-]colorize', 'colored output' do |v|
      options[:colorize] = v
    end
  end
  opt_parser.parse!(args)
  if args.empty?
    puts opt_parser.help
    exit
  end
  if options[:escape]
    options[:q] = Regexp.new(Regexp.escape(args.first))
  else
    options[:q] = Regexp.new(args.first)
  end
  options
end
working_dir() click to toggle source
# File lib/ruby_search/cli.rb, line 48
def working_dir
  ENV['PWD']
end