Class: GemDating::Cli

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

Constant Summary collapse

SUCCESS =
0
GENERAL_ERROR =
1
HELP_TEXT =
<<~HELP
  gem_dating [--help | -h] [<GEMFILE_FILEPATH>]

  GEMFILE_FILEPATH defaults to ./Gemfile if not provided.

  Options:
    --help, -h  Show this help message
HELP

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Cli

Returns a new instance of Cli.



16
17
18
19
20
21
# File 'lib/gem_dating/cli.rb', line 16

def initialize(argv = [])
  args, file_path = argv.partition { |arg| (arg =~ /--\w+/) || (arg =~ /(-[a-z])/) }

  @args = args
  @file_path = file_path.first
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gem_dating/cli.rb', line 23

def run
  if (@args & ['-h', '--help']).any?
    $stdout << HELP_TEXT
    return SUCCESS
  end

  unless @file_path
    current_directory = Dir.pwd
    file_path = File.join(current_directory, "Gemfile")

    if File.exist?(file_path)
      @file_path = file_path
    else
      $stdout << HELP_TEXT
      return GENERAL_ERROR
    end
  end

  $stdout << GemDating.from_file(@file_path).table_print << "\n"

  SUCCESS
end