Class: Fatty::Alert
- Inherits:
-
Object
- Object
- Fatty::Alert
- Defined in:
- lib/fatty/alert.rb
Constant Summary collapse
- DETAIL_ORDER =
%i[terminal key ctrl meta shift].freeze
Instance Attribute Summary collapse
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Class Method Summary collapse
-
.error(msg) ⇒ Alert
Return a new Alert object at level error.
-
.info(msg) ⇒ Alert
Return a new Alert object at level info.
-
.warn(msg) ⇒ Alert
Return a new Alert object at level warn.
Instance Method Summary collapse
-
#format ⇒ Object
Build a string version of the Alert suitable for display to the user.
-
#initialize(message:, level: :info, details: nil, sticky: false) ⇒ Alert
constructor
Return a new Alert object.
-
#role ⇒ Object
Translate the "level" to a "role" used by the renderers.
-
#sticky? ⇒ true, false
Return whether this Alert is sticky, meaning that it should not be cleared until a key is presses or another Alert displayed.
Constructor Details
#initialize(message:, level: :info, details: nil, sticky: false) ⇒ Alert
Return a new Alert object
16 17 18 19 20 21 |
# File 'lib/fatty/alert.rb', line 16 def initialize(message:, level: :info, details: nil, sticky: false) @message = @level = level.to_sym @details = details @sticky = !!sticky end |
Instance Attribute Details
#details ⇒ Object (readonly)
Returns the value of attribute details.
7 8 9 |
# File 'lib/fatty/alert.rb', line 7 def details @details end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
7 8 9 |
# File 'lib/fatty/alert.rb', line 7 def level @level end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
7 8 9 |
# File 'lib/fatty/alert.rb', line 7 def @message end |
Class Method Details
.error(msg) ⇒ Alert
Return a new Alert object at level error
43 44 45 |
# File 'lib/fatty/alert.rb', line 43 def self.error(msg) new(message: msg, level: :error) end |
.info(msg) ⇒ Alert
Return a new Alert object at level info
27 28 29 |
# File 'lib/fatty/alert.rb', line 27 def self.info(msg) new(message: msg, level: :info) end |
.warn(msg) ⇒ Alert
Return a new Alert object at level warn
35 36 37 |
# File 'lib/fatty/alert.rb', line 35 def self.warn(msg) new(message: msg, level: :warn) end |
Instance Method Details
#format ⇒ Object
Build a string version of the Alert suitable for display to the user.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/fatty/alert.rb', line 64 def format icon = case level when :warn then "⚠" when :error then "✖" else "ℹ" end msg = .to_s details_str = "" if details.respond_to?(:empty?) ? !details.empty? : !!details details_str = if details.is_a?(Hash) " (" + DETAIL_ORDER.filter_map { |k| "#{k}=#{details[k]}" if details.key?(k) }.join(" ") + ")" else " (#{details})" end end "#{icon} #{msg}#{details_str}" end |
#role ⇒ Object
Translate the "level" to a "role" used by the renderers. The returned roles are "composite" roles in the resolver in that they take the background ot the alert panel and apply a foreground color based on severity. @param level [:info, :warn, :error] @return [Symbol] composite or semantic role used by renderer (e.g., alert_good, :alert_info, :alert_warn, :alert_error)
54 55 56 57 58 59 60 61 |
# File 'lib/fatty/alert.rb', line 54 def role case level when :good then :alert_good when :warn then :alert_warn when :error then :alert_error else :alert_info end end |
#sticky? ⇒ true, false
Return whether this Alert is sticky, meaning that it should not be cleared until a key is presses or another Alert displayed
89 90 91 |
# File 'lib/fatty/alert.rb', line 89 def sticky? @sticky end |