Class: NRSER::Message
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
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.
- #options ⇒ Object
-
#send_to(receiver, publicly: true) ⇒ Object
Send this instance to a receiver object.
- #symbolize_options ⇒ 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.to_sym @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 (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
#options ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/nrser/message.rb', line 92 def if args.last.is_a? Hash args.last else {} end end |
#send_to(receiver, publicly: true) ⇒ Object
Send this instance to a receiver object.
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_options ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/nrser/message.rb', line 101 def 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.
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_s ⇒ String
Returns 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 |