Class: CLIUtils::Messenger
- Inherits:
-
Object
- Object
- CLIUtils::Messenger
- Includes:
- PrettyIO
- Defined in:
- lib/cliutils/messenger.rb
Overview
Allows for messages to be sent to STDOUT, as well as any number of Logger instances.
Instance Attribute Summary collapse
-
#targets ⇒ Array
readonly
The endpoints to which delegation occurs.
Instance Method Summary collapse
-
#attach(target) ⇒ void
Attaches a new target to delegate to.
-
#debug(m) ⇒ void
Outputs a debug message; since this shouldn’t appear in STDOUT, only send it to the Logger targets.
-
#detach(target_name) ⇒ void
Detaches a delegation target.
-
#error(m) ⇒ void
Outputs a formatted-red error message.
-
#info(m) ⇒ void
Outputs a formatted-blue informational message.
-
#info_block(*params) ⇒ void
Deprecated method to show info messages around a block of actions.
-
#initialize(targets = {}) ⇒ Messenger
constructor
Initializes a new Messenger with an optional Hash of targets.
-
#prompt(prompt, default = nil) ⇒ String
Outputs a prompt, collects the user’s response, and returns it.
-
#section(m) ⇒ void
Outputs a formatted-purple section message.
-
#success(m) ⇒ void
Outputs a formatted-green success message.
-
#warn(m) ⇒ void
Outputs a formatted-yellow warning message.
Methods included from PrettyIO
Constructor Details
#initialize(targets = {}) ⇒ Messenger
Initializes a new Messenger with an optional Hash of targets.
66 67 68 |
# File 'lib/cliutils/messenger.rb', line 66 def initialize(targets = {}) @targets = targets end |
Instance Attribute Details
#targets ⇒ Array (readonly)
The endpoints to which delegation occurs.
12 13 14 |
# File 'lib/cliutils/messenger.rb', line 12 def targets @targets end |
Instance Method Details
#attach(target) ⇒ void
This method returns an undefined value.
Attaches a new target to delegate to.
17 18 19 20 |
# File 'lib/cliutils/messenger.rb', line 17 def attach(target) fail "Cannot add invalid target: #{ target }" unless target.is_a?(Hash) @targets.merge!(target) end |
#debug(m) ⇒ void
This method returns an undefined value.
Outputs a debug message; since this shouldn’t appear in STDOUT, only send it to the Logger targets.
25 26 27 |
# File 'lib/cliutils/messenger.rb', line 25 def debug(m) @targets.each { |_, t| t.debug { m } } end |
#detach(target_name) ⇒ void
This method returns an undefined value.
Detaches a delegation target.
32 33 34 35 36 37 |
# File 'lib/cliutils/messenger.rb', line 32 def detach(target_name) unless @targets.key?(target_name) fail "Cannot detach invalid target: #{ target_name }" end @targets.delete(target_name) end |
#error(m) ⇒ void
This method returns an undefined value.
Outputs a formatted-red error message.
42 43 44 45 |
# File 'lib/cliutils/messenger.rb', line 42 def error(m) puts _word_wrap(m, '# ').red @targets.each { |_, t| t.error(m) } end |
#info(m) ⇒ void
This method returns an undefined value.
Outputs a formatted-blue informational message.
50 51 52 53 |
# File 'lib/cliutils/messenger.rb', line 50 def info(m) puts _word_wrap(m, '# ').blue @targets.each { |_, t| t.info(m) } end |
#info_block(*params) ⇒ void
This method returns an undefined value.
Deprecated method to show info messages around a block of actions
59 60 61 |
# File 'lib/cliutils/messenger.rb', line 59 def info_block(*params) warn('As of 2.2.0, `info_block` is deprecated and nonfunctioning.') end |
#prompt(prompt, default = nil) ⇒ String
Outputs a prompt, collects the user’s response, and returns it.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/cliutils/messenger.rb', line 75 def prompt(prompt, default = nil) Readline.completion_append_character = nil Readline.completion_proc = Proc.new do |str| Dir[str + '*'].grep( /^#{ Regexp.escape(str) }/ ) end p = "# #{ prompt }#{ default.nil? ? ':' : " [default: #{ default }]:" } " r = '' choice = Readline.readline(p.cyan) if choice.nil? || choice.empty? r = default else r = choice end @targets.each { |_, t| t.prompt("Answer to '#{ prompt }': #{ r }") } r end |
#section(m) ⇒ void
This method returns an undefined value.
Outputs a formatted-purple section message.
97 98 99 100 |
# File 'lib/cliutils/messenger.rb', line 97 def section(m) puts _word_wrap(m, '---> ').purple @targets.each { |_, t| t.section(m) } end |
#success(m) ⇒ void
This method returns an undefined value.
Outputs a formatted-green success message.
105 106 107 108 |
# File 'lib/cliutils/messenger.rb', line 105 def success(m) puts _word_wrap(m, '# ').green @targets.each { |_, t| t.success(m) } end |
#warn(m) ⇒ void
This method returns an undefined value.
Outputs a formatted-yellow warning message.
113 114 115 116 |
# File 'lib/cliutils/messenger.rb', line 113 def warn(m) puts _word_wrap(m, '# ').yellow @targets.each { |_, t| t.warn(m) } end |