Module: MultiSend

Defined in:
lib/multi_send.rb,
lib/multi_send/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.array(object, *messages) ⇒ Object

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


21
22
23
24
25
# 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) ⇒ Object

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


7
8
9
10
11
12
13
# 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 = {}) ⇒ Object

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] )


33
34
35
# File 'lib/multi_send.rb', line 33

def self.hash(object, messages = {})
  MultiSend.array(object, *messages.map { |message, args| [message, *args] })
end