Sha256: 08bdf04b24734a95ff784504772d27bb2a74c0c60822bc8dfebfe5bc6872c73d

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

require '../lib/Moby.rb'

Context::define :MoneyTransfer do
  role :source do
    withdraw do |amount|
      source.movement(amount)
      source.log "withdrawal #{amount}"
    end
    log do |message|
      p "#{@source} source #{message}"
    end
  end

  role :destination do
    deposit do |amount|
      @destination.movement(amount)
      @destination.log "deposit #{amount}"
    end
    logger do |message|
      p "#{@source} destination #{message}"
    end
  end

  role :amount do end

  transfer do
    source.withdraw -amount
    destination.deposit amount
  end
end

class MoneyTransfer
  def initialize(source, destination, amount)
    @source = source
    @destination = destination
    @amount  = amount
  end
end
class Account
  def initialize (amount, id)
    @balance = amount
    @account_id = id
  end

  def movement(amount)
    log "Amount #{amount}"
    @balance+=amount
  end

  def log(message)
    (p s = "instance #{message}")
  end

  def to_s
    "balance of #{@account_id}: #{@balance}"
  end
end

account = Account.new 1000, "source"
ctx = MoneyTransfer.new account, account, 100
ctx.transfer

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
Moby-0.5.1 Examples/MoneyTransfer.rb
Moby-0.5.0 Examples/MoneyTransfer.rb