Class: Fatty::Alert

Inherits:
Object
  • Object
show all
Defined in:
lib/fatty/alert.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, role: :info, details: nil, sticky: false) ⇒ Alert

Return a new Alert object

Parameters:

  • message (String)
  • role (:good, :info, :warn, :error) (defaults to: :info)
  • details (Hash|String) (defaults to: nil)
  • sticky (Boolean) (defaults to: false)


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

#detailsObject (readonly)

Returns the value of attribute details.



5
6
7
# File 'lib/fatty/alert.rb', line 5

def details
  @details
end

#roleObject (readonly)

Returns the value of attribute role.



5
6
7
# File 'lib/fatty/alert.rb', line 5

def role
  @role
end

#textObject (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

Parameters:

  • msg (String)

Returns:

  • (Alert)

    with 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

Parameters:

  • msg (String)

Returns:

  • (Alert)

    with 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

Parameters:

  • msg (String)

Returns:

  • (Alert)

    with 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

Parameters:

  • msg (String)

Returns:

  • (Alert)

    with 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_orderObject



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

#formatObject

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

#inspectObject



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

Returns:

  • (true, false)

    is this Alert sticky?



104
105
106
# File 'lib/fatty/alert.rb', line 104

def sticky?
  @sticky
end