module MultiSend
Constants
- VERSION
Public Class Methods
array(object, *messages)
click to toggle source
Sends an array of messages to self. Used like:
5.send_array(:days, :ago) => (datetime object)
Can specify arguments by using a nested array:
5.send_array( [ :+, 8 ], [ :*, 9] ) => 77
# File lib/multi_send.rb, line 21 def self.array(object, *messages) messages.reduce(object) do |obj, message| obj.__send__(*message) end end
do(object, *messages)
click to toggle source
automagically figure out whether to send as a hash or array.
>> 5.multi_send( :days, :ago, , :to_date, [:eql?, 5.days.ago.to_date] ) => true
# File lib/multi_send.rb, line 7 def self.do(object, *messages) if messages.length == 1 && messages[0].is_a?(Hash) MultiSend.hash(object, messages[0]) else MultiSend.array(object, *messages) end end
hash(object, messages = {})
click to toggle source
Sends a hash of messages to self. The keys in the hash are the message to send to self and the values are the arguments. Used like:
5.send_hash( :+ => 5, eql?: 10 ) => true
Multiple arguments can be sent as an array:
5.send_hash( :+ => 5, send: [:eql?, 10] )
# File lib/multi_send.rb, line 33 def self.hash(object, messages = {}) MultiSend.array(object, *messages.map { |message, args| [message, *args] }) end
Public Instance Methods
multi_send(*messages)
click to toggle source
# File lib/multi_send.rb, line 49 def multi_send(*messages) MultiSend.do(self, *messages) end
send_array(*messages)
click to toggle source
# File lib/multi_send.rb, line 41 def send_array(*messages) MultiSend.array(self, *messages) end
send_hash(messages = {})
click to toggle source
# File lib/multi_send.rb, line 45 def send_hash(messages = {}) MultiSend.hash(self, messages) end