Class: Fatty::Alert
- Inherits:
-
Object
- Object
- Fatty::Alert
- Defined in:
- lib/fatty/alert.rb
Instance Attribute Summary collapse
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Class Method Summary collapse
-
.error(msg) ⇒ Alert
Return a new Alert object at role error.
-
.good(msg) ⇒ Alert
Return a new Alert object at role good.
-
.info(msg) ⇒ Alert
Return a new Alert object at role :info.
-
.warn(msg) ⇒ Alert
Return a new Alert object at role :warn.
Instance Method Summary collapse
- #detail_key_order ⇒ Object
-
#format ⇒ Object
Build a string version of the Alert suitable for display to the user.
-
#initialize(text:, role: :info, details: nil, sticky: false) ⇒ Alert
constructor
Return a new Alert object.
- #inspect ⇒ Object
-
#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(text:, role: :info, details: nil, sticky: false) ⇒ Alert
Return a new Alert object
14 15 16 17 18 19 |
# File 'lib/fatty/alert.rb', line 14 def initialize(text:, role: :info, details: nil, sticky: false) @text = text @role = role.to_sym @details = details @sticky = !!sticky end |
Instance Attribute Details
#details ⇒ Object (readonly)
Returns the value of attribute details.
5 6 7 |
# File 'lib/fatty/alert.rb', line 5 def details @details end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
5 6 7 |
# File 'lib/fatty/alert.rb', line 5 def role @role end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
5 6 7 |
# File 'lib/fatty/alert.rb', line 5 def text @text end |
Class Method Details
.error(msg) ⇒ Alert
Return a new Alert object at role error
57 58 59 |
# File 'lib/fatty/alert.rb', line 57 def self.error(msg) new(text: msg, role: :error) end |
.good(msg) ⇒ Alert
Return a new Alert object at role good
33 34 35 |
# File 'lib/fatty/alert.rb', line 33 def self.good(msg) new(text: msg, role: :good) end |
.info(msg) ⇒ Alert
Return a new Alert object at role :info
41 42 43 |
# File 'lib/fatty/alert.rb', line 41 def self.info(msg) new(text: msg, role: :info) end |
.warn(msg) ⇒ Alert
Return a new Alert object at role :warn
49 50 51 |
# File 'lib/fatty/alert.rb', line 49 def self.warn(msg) new(text: msg, role: :warn) end |
Instance Method Details
#detail_key_order ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/fatty/alert.rb', line 108 def detail_key_order keys = [] keys << :terminal if details.key?(:terminal) keys << :key if details.key?(:key) keys << :shift if details.key?(:shift) keys << :ctrl if details.key?(:ctrl) keys << :meta if details.key?(:meta) other_keys = details.keys - [:terminal, :key, :shift, :ctrl, :meta] keys + other_keys.sort end |
#format ⇒ Object
Build a string version of the Alert suitable for display to the user.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fatty/alert.rb', line 78 def format icon = case role when :good then " ✔ " when :info then " ℹ " when :warn then " ⚠ " when :error then " ✖ " else " " end details_str = if details.nil? || details.empty? "" else key_strs = [] detail_key_order.each do |k| key_strs << "#{k}=#{details[k]}" end " (#{key_strs.join(' ')})" end "#{icon} #{text}#{details_str}" end |
#inspect ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/fatty/alert.rb', line 21 def inspect txt = +"Alert: " txt << "text: #{@text}; " if @text txt << "role: #{@role}; " if @role txt << "details: #{@details}; " if @details txt << "sticky: #{@sticky}." 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
104 105 106 |
# File 'lib/fatty/alert.rb', line 104 def sticky? @sticky end |