Class: Fatty::Alert

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

Constant Summary collapse

DETAIL_ORDER =
%i[terminal key ctrl meta shift].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message:, level: :info, details: nil, sticky: false) ⇒ Alert

Return a new Alert object

Parameters:

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


16
17
18
19
20
21
# File 'lib/fatty/alert.rb', line 16

def initialize(message:, level: :info, details: nil, sticky: false)
  @message = message
  @level = level.to_sym
  @details = details
  @sticky  = !!sticky
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



7
8
9
# File 'lib/fatty/alert.rb', line 7

def details
  @details
end

#levelObject (readonly)

Returns the value of attribute level.



7
8
9
# File 'lib/fatty/alert.rb', line 7

def level
  @level
end

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/fatty/alert.rb', line 7

def message
  @message
end

Class Method Details

.error(msg) ⇒ Alert

Return a new Alert object at level error

Parameters:

Returns:

  • (Alert)

    with 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

Parameters:

Returns:

  • (Alert)

    with 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

Parameters:

Returns:

  • (Alert)

    with 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

#formatObject

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 = message.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

#roleObject

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

Returns:

  • (true, false)

    is this Alert sticky?



89
90
91
# File 'lib/fatty/alert.rb', line 89

def sticky?
  @sticky
end