method-combinators
method-combinators copied to clipboard
added actsAs combinator
Okay, this was a fun xmas one while my toddler is napping.
I used your around combinator to build an actsAs combinator. This assigns roles a la DCI.
So the equivalent of this ruby DCI example:
def initialize source_account, destination_account
@source_account = source_account.extend SourceAccount
@destination_account = destination_account.extend DestinationAccount
doStuff...
end
is the hotter
initialize: actsAs(SourceAccount, DestinationAccount) (@sourceAccount, @destinationaccount)->
doStuff...
This has the big benefit of removing the role after the method invocation (whereas in the ruby example the modules crash the inheritance party). I named it actAs as an obvious "shout out".
Also, actsAs takes objects as input and steals their methods for the target objects. If the parameters to actsAs are instead classes, then it steals their class methods, not instance methods.