Module: PostDB::CLI::Helper
- Defined in:
- lib/postdb/cli/helper.rb
Overview
The helper CLI module
This module contains helper functionality for the CLI.
Instance Method Summary collapse
-
#confirm_action!(message, aborted_message = nil) ⇒ Object
Confirm an action.
-
#exit_with_error(*messages) ⇒ Object
Exit with an error.
-
#exit_with_warning(*messages) ⇒ Object
Exit with a warning.
-
#format_error(error) ⇒ Object
Format an error.
-
#new_line ⇒ Object
Print a newline character.
-
#prompt ⇒ Object
Get a prompt object.
Instance Method Details
#confirm_action!(message, aborted_message = nil) ⇒ Object
Confirm an action
Arguments:
message: (String)
aborted_message: (String)
Example:
>> confirm_action! "Do something?"
=> nil
72 73 74 75 76 77 78 |
# File 'lib/postdb/cli/helper.rb', line 72 def confirm_action!(, = nil) # Prompt the user to confirm the action if prompt.no?() # Exit with a warning exit_with_warning() end end |
#exit_with_error(*messages) ⇒ Object
Exit with an error
Arguments:
messages: (Splat)
Example:
>> exit_with_error "Oops!"
=> nil
37 38 39 40 41 42 43 |
# File 'lib/postdb/cli/helper.rb', line 37 def exit_with_error(*) .each do || ("\n" == ) ? print() : prompt.error() end exit 1 end |
#exit_with_warning(*messages) ⇒ Object
Exit with a warning
Arguments:
messages: (Splat)
Example:
>> exit_with_warning "Oops!"
=> nil
54 55 56 57 58 59 60 |
# File 'lib/postdb/cli/helper.rb', line 54 def exit_with_warning(*) .each do || ("\n" == ) ? print() : prompt.warn() end exit 1 end |
#format_error(error) ⇒ Object
Format an error
Arguments:
error: (Exception)
Example:
>> format_error(error)
=> ["\n", "Error:", " Error Message", "Backtrace:", " from (irb):1"]
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/postdb/cli/helper.rb', line 89 def format_error(error) errors = Array.new errors << "\n" errors << "Error:" errors << " " + error. errors << "\n" errors << "Backtrace:" errors += error.backtrace.map { |trace| " " + trace } errors end |
#new_line ⇒ Object
Print a newline character
Example:
>> new_line
=> "\n"
14 15 16 |
# File 'lib/postdb/cli/helper.rb', line 14 def new_line puts "" end |
#prompt ⇒ Object
Get a prompt object
Example:
>> prompt
=> #<TTY::Prompt:0x00000000000000>
24 25 26 |
# File 'lib/postdb/cli/helper.rb', line 24 def prompt @prompt ||= TTY::Prompt.new end |