Class: RubySearch::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_search/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



17
18
19
# File 'lib/ruby_search/cli.rb', line 17

def initialize
  @options = parse_options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/ruby_search/cli.rb', line 15

def options
  @options
end

Class Method Details

.runObject



6
7
8
9
10
11
12
13
# 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

Instance Method Details

#bundler_groupsObject



52
53
54
55
# 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_environmentObject



57
58
59
60
61
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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_dirObject



48
49
50
# File 'lib/ruby_search/cli.rb', line 48

def working_dir
  ENV['PWD']
end