Class: NRSER::Message
- Inherits:
-
Object
- Object
- NRSER::Message
- 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 &
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
-
#args ⇒ Array
readonly
Arguments (parameters).
-
#block ⇒ nil | #call
readonly
Optional block to send to the receiver.
-
#symbol ⇒ Symbol | String
readonly
Name of method the message is for.
Class Method Summary collapse
-
.from(*args, &block) ⇒ Object
Instantiate a message from the arguments, unless they already are one.
Instance Method Summary collapse
-
#initialize(symbol, *args, &block) ⇒ Message
constructor
Construct a new message.
-
#send_to(receiver, publicly: true) ⇒ Object
Send this instance to a receiver object.
-
#to_proc(publicly: true) ⇒ Proc
(also: #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.
-
#to_s ⇒ String
Brief description of the message.
Constructor Details
#initialize(symbol, *args, &block) ⇒ Message
Construct a new message.
85 86 87 88 89 |
# File 'lib/nrser/message.rb', line 85 def initialize symbol, *args, &block @symbol = symbol @args = args @block = block end |
Instance Attribute Details
#args ⇒ Array (readonly)
Arguments (parameters). May be empty.
64 65 66 |
# File 'lib/nrser/message.rb', line 64 def args @args end |
#block ⇒ nil | #call (readonly)
Optional block to send to the receiver.
71 72 73 |
# File 'lib/nrser/message.rb', line 71 def block @block end |
#symbol ⇒ Symbol | String (readonly)
Name of method the message is for.
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.
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
#send_to(receiver, publicly: true) ⇒ Object
Send this instance to a receiver object.
138 139 140 141 142 143 144 |
# File 'lib/nrser/message.rb', line 138 def send_to receiver, publicly: true if publicly receiver.public_send symbol, *args, &block else receiver.send symbol, *args, &block 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.
111 112 113 114 |
# File 'lib/nrser/message.rb', line 111 def to_proc publicly: true # block ->( receiver ) { send_to receiver, publicly: publicly } end |
#to_s ⇒ String
Returns Brief description of the message.
149 150 151 |
# File 'lib/nrser/message.rb', line 149 def to_s "#<NRSER::Message symbol=#{ symbol } args=#{ args } block=#{ block }>" end |