public
Created

Context & Roles

  • Download Gist
context_and_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 29 30 31 32 33 34 35 36 37 38 39 40 41
class TransferringMoney
include Context
 
def self.transfer source_account_id, destination_account_id, amount
source = Account.find(source_account_id)
destination = Account.find(destination_account_id)
TransferringMoney.new(source, destination).transfer amount
end
 
attr_reader :source_account, :destination_account
def initialize source_account, destination_account
@source_account = source_account.extend SourceAccount
@destination_account = destination_account.extend DestinationAccount
end
 
def transfer amount
in_context do
source_account.transfer_out amount
end
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.