Class: Message

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/message.rb,
lib/utils/message.rb

Overview

Private Methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, replaces: nil) ⇒ Message

Initializes a new instance of the Message class with a given message template.

Parameters:

  • message (String)

    The message template to be used.

  • replaces (Hash) (defaults to: nil)

    Used to replace all occurrences of a key with its value.



24
25
26
27
28
29
# File 'lib/utils/message.rb', line 24

def initialize(message, replaces: nil)
  raise 'Messsage replaces content need be a Hash' if !replaces.nil? && !replaces.is_a?(Hash)

  @to_replace = replaces
  @message = message
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



18
19
20
# File 'lib/utils/message.rb', line 18

def message
  @message
end

Instance Method Details

#==(other) ⇒ Boolean

Compares two Message instances for equality based on their message content.

Parameters:

  • other (Message)

    The other Message instance to compare with.

Returns:

  • (Boolean)

    True if the messages are equal, otherwise false.



35
36
37
# File 'lib/utils/message.rb', line 35

def ==(other)
  self.class == other.class && @message == other.message
end

#to_sString

Converts the message template into a string, replacing any placeholders with actual data. This method searches for keys within the message and replaces them with corresponding content from message files located in either the custom path or the library path and appling the given replaces.

Returns:

  • (String)

    The formatted message with placeholders substituted with actual content.



45
46
47
48
# File 'lib/utils/message.rb', line 45

def to_s
  new_message = replace_message_keys(String.new(@message))
  replace_all_to_replace_elements(new_message)
end