module Hexx # A message to be returned by services. class Message include Comparable # @!attribute [r] type # The type of the message # # @example # message = Message.new type: :error, text: "message" # message.type = "error" # # @return [String] The type of the message. attr_reader :type # @!attribute [r] text # The text of the message # # @example # message = Message.new type: :error, text: "message" # message.text = "message" # # @return [String] The text of the message. attr_reader :text # @!scope class # @!method new(options) # Constructs the message with type and text. # # @example # Message.new type: "success", text: "Object created." # # @param [Hash] options The list of the message attributes. # @option options [String, Symbol] :type The type of the message. # @option options [String, Symbol] :text The text of the message. # @return [Hexx::Message] The message. # @api hide def initialize(type:, text:) @type, @text = type.to_s, text.to_s end # Distinguishes two messages by type and text. # # @example # a = Message.new(type: "a", text: "a") # b = Message.new(type: "a", text: "a") # c = Message.new(type: "b", text: "a") # d = Message.new(type: "a", text: "b") # # a == b # => true # a == c # => false # a == d # => false # # @param [Object] other The object for the comparison. # @return [Boolean] The result of the comparison. def ==(other) return false unless other.is_a? self.class [type, text] == [other.type, other.text] end # Compares messages by type and text. # # @example # ab = Message.new(type: "a", text: "b") # ba = Message.new(type: "b", text: "a") # ab < ba # => true # # @param [Object] other The object for the comparison. # @return [-1, 0, 1] The result of the comparison if the argument is # comparable with the message. # @return [nil] if the result is incomparable with the message. def <=>(other) fail ArgumentError unless other.is_a? self.class [type, text] <=> [other.type, other.text] end end end