Skip to content

Instantly share code, notes, and snippets.

@quad
Last active December 10, 2015 19:48
Show Gist options
  • Select an option

  • Save quad/4483530 to your computer and use it in GitHub Desktop.

Select an option

Save quad/4483530 to your computer and use it in GitHub Desktop.
Working through the DCI example from Singapore Ruby (2013-1-8)
require 'minitest/autorun'
class Account < Struct.new :balance
end
class MoneyTransfer < Struct.new :source, :destination
def initialize source, destination
super
source.extend Transferrer
end
def execute amount
source.transfer_to destination, amount
end
private
module Transferrer
def transfer_to destination, amount
self.balance -= amount
destination.balance += amount
end
end
end
describe MoneyTransfer do
let(:rich_account) { Account.new 100 }
let(:poor_account) { Account.new 25 }
before do
transfer_context = MoneyTransfer.new rich_account, poor_account
transfer_context.execute 25
end
it('should redistrict wealth from the rich man') { rich_account.balance.must_equal 75 }
it('to the poor man') { poor_account.balance.must_equal 50 }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment