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

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!(message, aborted_message = nil)
  # Prompt the user to confirm the action
  if prompt.no?(message)
    # Exit with a warning
    exit_with_warning(aborted_message)
  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(*messages)
  messages.each do |message|
    ("\n" == message) ? print(message) : prompt.error(message)
  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(*messages)
  messages.each do |message|
    ("\n" == message) ? print(message) : prompt.warn(message)
  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.message
  errors << "\n"
  errors << "Backtrace:"
  errors += error.backtrace.map { |trace| "  " + trace }
  errors
end

#new_lineObject

Print a newline character

Example:

>> new_line
=> "\n"


14
15
16
# File 'lib/postdb/cli/helper.rb', line 14

def new_line
  puts ""
end

#promptObject

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