Class: Dsu::Views::Shared::Message

Inherits:
Object
  • Object
show all
Includes:
Support::ColorThemable
Defined in:
lib/dsu/views/shared/message.rb

Direct Known Subclasses

Error, Info, Success, Warning

Constant Summary collapse

MESSAGE_TYPES =
i[error info success warning].freeze

Instance Method Summary collapse

Methods included from Support::ColorThemable

apply_theme, #prompt_with_options

Constructor Details

#initialize(messages:, message_type:, options: {}) ⇒ Message

Returns a new instance of Message.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dsu/views/shared/message.rb', line 13

def initialize(messages:, message_type:, options: {})
  messages = [messages] unless messages.is_a?(Array)
  messages = messages.select(&:present?)

  validate_arguments!(messages, message_type, options)

  @messages = messages
  @message_type = message_type
  @options = options || {}
  @message_color = color_theme.public_send(message_type)
  @header = options[:header]
  @ordered_list = options.fetch(:ordered_list, true)
end

Instance Method Details

#renderObject



27
28
29
# File 'lib/dsu/views/shared/message.rb', line 27

def render
  output_stream.puts to_s
end

#to_sObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dsu/views/shared/message.rb', line 31

def to_s
  return if messages.empty?

  strings = []

  strings << apply_theme(header, theme_color: color_theme.header) if header.present?

  strings << if messages.one?
    apply_theme(messages[0], theme_color: message_color)
  else
    messages.each_with_index.map do |message, index|
      message = "#{index + 1}. #{message}" if ordered_list?
      apply_theme(message, theme_color: message_color)
    end
  end

  strings.flatten.join("\n")
end