Class: BenchPress::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.


6
7
8
9
10
11
# File 'lib/bench_press/cli.rb', line 6

def initialize(argv)
  @argv = argv
  @options = {}
  optparse.parse!(argv)
  @filename = argv.shift
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv


4
5
6
# File 'lib/bench_press/cli.rb', line 4

def argv
  @argv
end

#filenameObject (readonly)

Returns the value of attribute filename


4
5
6
# File 'lib/bench_press/cli.rb', line 4

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options


4
5
6
# File 'lib/bench_press/cli.rb', line 4

def options
  @options
end

Instance Method Details

#emailObject


13
14
15
# File 'lib/bench_press/cli.rb', line 13

def email
  options[:email] || git_email
end

#file_exists?Boolean

Returns:

  • (Boolean)

21
22
23
# File 'lib/bench_press/cli.rb', line 21

def file_exists?
  filename && File.exists?(file_path)
end

#file_pathObject


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

def file_path
  @file_path ||= File.join(Dir.pwd, filename)
end

#git_emailObject


25
26
27
# File 'lib/bench_press/cli.rb', line 25

def git_email
  @git_email ||= %x(git config --global --get user.email).strip
end

#optparseObject


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bench_press/cli.rb', line 29

def optparse
  @optparse ||= OptionParser.new do |opts|
    opts.banner = "Usage: bench_press [options] file_to_benchmark.rb"

    opts.on( '-n', '--new', 'Create a new benchmark' ) do
      options[:new] = true
    end

    opts.on( '-p', '--publish', 'Publish the benchmark to http://rubybenchmark.com' ) do
      options[:publish] = true
    end

    opts.on('--email EMAIL', String, "Author email used when publishing, defaults to #{git_email}") do |email|
      options[:email] = email
    end

    opts.on( '-v', '--version', 'Show the version of bench_press' ) do
      options[:version] = true
    end
  end
end

#runObject


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bench_press/cli.rb', line 51

def run
  if options[:version]
    exit_with_version
  elsif file_exists?
    perform_bench_press
  elsif options[:new]
    new_bench_press
  else
    abort "Could not proceed, please supply the filename you wish to benchmark"
  end
end