Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Account
- attr_reader :account_id, :balance
- def initialize(account_id)
- @account_id = account_id
- @balance = 0
- end
- def withdraw(amount)
- raise "Insufficient funds" if amount < 0
- @balance -= amount
- end
- def deposit(amount)
- @balance += amount
- end
- def update_log(msg, date, amount)
- puts "Account: #{inspect}, #{msg}, #{date.to_s}, #{amount}"
- end
- def self.find(account_id)
- @@store ||= Hash.new
- return @@store[account_id] if @@store.has_key? account_id
- if :savings == account_id
- account = SavingsAccount.new(account_id)
- account.deposit(100000)
- elsif :checking == account_id
- account = CheckingAccount.new(account_id)
- else
- account = Account.new(account_id)
- end
- @@store[account_id] = account
- account
- end
- end
- module MoneySource
- include Rdci::Role
- def transfer_out
- raise "Insufficient funds" if balance < Amount
- withdraw Amount
- DestinationAccount.deposit Amount
- update_log "Transfer Out", Time.now, Amount
- DestinationAccount.update_log "Transfer In", Time.now, Amount
- end
- end
- class TransferMoneyContext
- include Rdci::Context
- attr_reader :source_account, :destination_account, :amount
- def self.execute(amt, source_account_id, destination_account_id)
- TransferMoneyContext.new(amt, source_account_id, destination_account_id).execute
- end
- def initialize(amt, source_account_id, destination_account_id)
- @source_account = Account.find(source_account_id)
- @source_account.mixin MoneySource
- @destination_account = Account.find(destination_account_id)
- @amount = amt
- end
- def execute
- in_context do
- source_account.transfer_out
- end
- @source_account.unmix MoneySource
- end
- end
- class SavingsAccount < Account
- end
- class CheckingAccount < Account
- end
- TransferMoneyContext.execute(300, :savings, :checking)
- puts "Savings: #{Account.find(:savings).balance}, Checking: #{Account.find(:checking).balance}"
Add Comment
Please, Sign In to add comment