Class: Mongify::CLI::Application

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

Overview

Represents an instance of a Mongify application. This is the entry point for all invocations of Mongify from the command line.

Constant Summary collapse

STATUS_SUCCESS =

Successful execution exit code

0
STATUS_ERROR =

Failed execution exit code

1

Instance Method Summary collapse

Constructor Details

#initialize(arguments = [], stdin = $stdin, stdout = $stdout) ⇒ Application

Returns a new instance of Application.



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

def initialize(arguments=[], stdin=$stdin, stdout=$stdout)
  arguments = ['-h'] if arguments.empty?
  @options = Options.new(arguments)
  @status = STATUS_SUCCESS
  Mongify::Configuration.in_stream = stdin
  Mongify::Configuration.out_stream = stdout
end

Instance Method Details

#execute!Object

Runs the application



25
26
27
28
29
30
31
32
33
# File 'lib/mongify/cli/application.rb', line 25

def execute!
  begin
    cmd = @options.parse
    return cmd.execute(self)
  rescue MongifyError => error
    $stderr.puts "Error: #{error}"
    report_error
  end
end

#output(message) ⇒ Object

Sends output to the UI



36
37
38
# File 'lib/mongify/cli/application.rb', line 36

def output(message)
  UI.puts(message)
end

#report_errorObject

Sets status code as failure (or error)



46
47
48
# File 'lib/mongify/cli/application.rb', line 46

def report_error
  @status = STATUS_ERROR
end

#report_successObject

Sets status code as successful



41
42
43
# File 'lib/mongify/cli/application.rb', line 41

def report_success
  @status = STATUS_SUCCESS
end