public
Created

Roles

  • Download Gist
roles.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
class TransferringMoney
include Context
...
def transfer amount
...
end
 
module SourceAccount
include ContextAccssor
 
def transfer_out amount
raise "Insufficient funds" if balance < amount
decrease_balance amount
context.destination_account.transfer_in amount
update_log "Transferred out", amount
end
end
 
module DestinationAccount
include ContextAccssor
 
def transfer_in amount
increase_balance amount
update_log "Transferred in", amount
end
end
end

Not sure if I'm a fan of the roles accessing the context. It feels like a bit of a boundary issue to me and it's unnecessary. Why is context.destination_account.transfer_in better than:

source_account.transfer_to(destination_account, amount)
...
def transfer_to destination, amount
  ...
  destination.receive_from(self, amount)
  ...
end

The other odd thing is both roles have the ContextAccssor mixin, but only one even uses it.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.