Class: NRSER::Message

Inherits:
Object show all
Defined in:
lib/nrser/message.rb

Overview

Container for a message (method call) to be sent to a receiver via Object#send (or Object#public_send).

Encapsulates the method symbol as well as any arguments and block to send.

Implements ‘#to_proc` so it can be used like

enum.map &message

You can invoke the message on a receiver object like

msg.send_to obj

Useful for clearly describing and recognizing data that is meant to be sent to an object as a method call, especially in testing.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, *args, &block) ⇒ Message

Construct a new message.

Parameters:

  • symbol (String | Symbol)

    Name of target method.

  • args (Array)

    Any arguments that should be sent.

  • block (nil | #call)

    Optional block that should be sent.



85
86
87
88
89
# File 'lib/nrser/message.rb', line 85

def initialize symbol, *args, &block
  @symbol = symbol.to_sym
  @args = args
  @block = block
end

Instance Attribute Details

#argsArray (readonly)

Arguments (parameters). May be empty.

Returns:



64
65
66
# File 'lib/nrser/message.rb', line 64

def args
  @args
end

#blocknil | #call (readonly)

Optional block to send to the receiver.

Returns:

  • (nil | #call)


71
72
73
# File 'lib/nrser/message.rb', line 71

def block
  @block
end

#symbolSymbol (readonly)

Name of method the message is for.

Returns:



57
58
59
# File 'lib/nrser/message.rb', line 57

def symbol
  @symbol
end

Class Method Details

.from(symbol, *args, &block) ⇒ NRSER::Message .from(message) ⇒ NRSER::Message

Instantiate a message from the arguments, unless they already are one.

Overloads:

  • .from(symbol, *args, &block) ⇒ NRSER::Message

    Create a new instance from the arguments by passing them to new.

    Parameters:

    • message (NRSER::Message)

      An already instantiated message, which is simple returned.

    Returns:

  • .from(message) ⇒ NRSER::Message

    Convenience method to return the message if it’s the only argument.

    Parameters:

    • message (NRSER::Message)

      An already instantiated message, which is simple returned.

    Returns:



44
45
46
47
48
49
50
# File 'lib/nrser/message.rb', line 44

def self.from *args, &block
  if args.length == 1 && args[0].is_a?( Message )
    args[0]
  else
    new *args, &block
  end
end

Instance Method Details

#optionsObject



92
93
94
95
96
97
98
# File 'lib/nrser/message.rb', line 92

def options
  if args.last.is_a? Hash
    args.last
  else
    {}
  end
end

#send_to(receiver, publicly: true) ⇒ Object

Send this instance to a receiver object.

Examples:


msg.send_to obj

Parameters:

  • receiver (Object)

    Object that the message will be sent to.

  • publicly (Boolean) (defaults to: true)

    When ‘true`, the message will be sent via Object#public_send. This is the default behavior.

    When ‘false`, the message will be sent via Object#send, allowing it to invoke private and protected methods on the receiver.

Returns:

  • (Object)

    Result of the method call.



159
160
161
162
163
164
165
# File 'lib/nrser/message.rb', line 159

def send_to receiver, publicly: true
  if publicly
    receiver.public_send symbol, *args, &block
  else
    receiver.send symbol, *args, &block
  end
end

#symbolize_optionsObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/nrser/message.rb', line 101

def symbolize_options
  if args.last.is_a? Hash
    self.class.new \
      symbol,
      *args[0..-2],
      args.last.sym_keys
  else
    self
  end
end

#to_proc(publicly: true) ⇒ Proc Also known as: to_sender

Creates a Proc that accepts a single ‘receiver` argument and calls #sent_to on it, allowing messages to be used via the `&` operator in `map`, etc.

Examples:

Map each entry as the message receiver using ‘&`


enum = [ [], [1], [1, 2] ]

length_message = NRSER::Message.new :length
first_message = NRSER::Message.new :first

enum.map &length_message
# => [0, 1, 2]

enum.map &first_message
# => [nil, 1, 1]

Returns:

  • (Proc)


132
133
134
135
# File 'lib/nrser/message.rb', line 132

def to_proc publicly: true
  # block
  ->( receiver ) { send_to receiver, publicly: publicly }
end

#to_sString

Returns Brief description of the message.

Returns:

  • (String)

    Brief description of the message.



170
171
172
# File 'lib/nrser/message.rb', line 170

def to_s
  "#<NRSER::Message symbol=#{ symbol } args=#{ args } block=#{ block }>"
end