Class: Message
- Inherits:
-
Object
- Object
- Message
- Defined in:
- lib/utils/message.rb
Overview
The Message class represents a mechanism for dynamically handling and formatting messages.
It supports the substitution of placeholders within a message template with actual data.
The class leverages file-based message templates, allowing for easy localization or customization of messages.
It integrates seamlessly with the Resources module to access these templates from a customizable path set via the "SCRIPT_CUSTOM_RESOURCES" environment variable or from a default library path.
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares two Message instances for equality based on their message content.
-
#initialize(message, replaces: nil) ⇒ Message
constructor
Initializes a new instance of the Message class with a given message template.
-
#to_s ⇒ String
Converts the message template into a string, replacing any placeholders with actual data.
Constructor Details
#initialize(message, replaces: nil) ⇒ Message
Initializes a new instance of the Message class with a given message template.
28 29 30 31 32 33 |
# File 'lib/utils/message.rb', line 28 def initialize(, replaces: nil) raise 'Messsage replaces content need be a Hash' if !replaces.nil? && !replaces.is_a?(Hash) @to_replace = replaces @message = end |
Instance Attribute Details
#message ⇒ Object (readonly)
Returns the value of attribute message.
22 23 24 |
# File 'lib/utils/message.rb', line 22 def @message end |
Instance Method Details
#==(other) ⇒ Boolean
Compares two Message instances for equality based on their message content.
39 40 41 |
# File 'lib/utils/message.rb', line 39 def ==(other) self.class == other.class && @message == other. end |
#to_s ⇒ String
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.
49 50 51 52 |
# File 'lib/utils/message.rb', line 49 def to_s = (String.new(@message)) replace_all_to_replace_elements() end |