Module: MultiSend
- Defined in:
- lib/multi_send.rb,
lib/multi_send/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.array(object, *messages) ⇒ Object
Sends an array of messages to self.
-
.do(object, *messages) ⇒ Object
automagically figure out whether to send as a hash or array.
-
.hash(object, messages = {}) ⇒ Object
Sends a hash of messages to self.
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, *) .reduce(object) do |obj, | obj.__send__(*) 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, *) if .length == 1 && [0].is_a?(Hash) MultiSend.hash(object, [0]) else MultiSend.array(object, *) 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, = {}) MultiSend.array(object, *.map { |, args| [, *args] }) end |