Class: Hexx::Message

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/hexx/message.rb

Overview

A message to be returned by services.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#textString (readonly)

The text of the message

Examples:

message = Message.new type: :error, text: "message"
message.text = "message"

Returns:

  • (String)

    The text of the message.


25
26
27
# File 'lib/hexx/message.rb', line 25

def text
  @text
end

#typeString (readonly)

The type of the message

Examples:

message = Message.new type: :error, text: "message"
message.type = "error"

Returns:

  • (String)

    The type of the message.


15
16
17
# File 'lib/hexx/message.rb', line 15

def type
  @type
end

Class Method Details

.new(options) ⇒ Hexx::Message

Constructs the message with type and text.

Examples:

Message.new type: "success", text: "Object created."

Parameters:

  • options (Hash)

    The list of the message attributes.

Options Hash (options):

  • :type (String, Symbol)

    The type of the message.

  • :text (String, Symbol)

    The text of the message.

Returns:


# File 'lib/hexx/message.rb', line 27

Instance Method Details

#<=>(other) ⇒ -1, ...

Compares messages by type and text.

Examples:

ab = Message.new(type: "a", text: "b")
ba = Message.new(type: "b", text: "a")
ab < ba # => true

Parameters:

  • other (Object)

    The object for the comparison.

Returns:

  • (-1, 0, 1)

    The result of the comparison if the argument is comparable with the message.

  • (nil)

    if the result is incomparable with the message.


74
75
76
77
# File 'lib/hexx/message.rb', line 74

def <=>(other)
  fail ArgumentError unless other.is_a? self.class
  [type, text] <=> [other.type, other.text]
end

#==(other) ⇒ Boolean

Distinguishes two messages by type and text.

Examples:

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

Parameters:

  • other (Object)

    The object for the comparison.

Returns:

  • (Boolean)

    The result of the comparison.


58
59
60
61
# File 'lib/hexx/message.rb', line 58

def ==(other)
  return false unless other.is_a? self.class
  [type, text] == [other.type, other.text]
end