Skip to content

Instantly share code, notes, and snippets.

@maxnik
Created September 11, 2012 13:33
Show Gist options
  • Select an option

  • Save maxnik/3698496 to your computer and use it in GitHub Desktop.

Select an option

Save maxnik/3698496 to your computer and use it in GitHub Desktop.
Data Context Interaction (DCI) пример с TransferringMoney
# Я решил оставить его исходный интерфейс для класса Account, будем считать,
# что я получил это код от предшественника и теперь мне надо реализовать
# перевод денег с одно счета на другой:
class Account
def decrease_balance(amount); end
def increase_balance(amount); end
def balance; end
def update_log(message, amount); end
def self.find(id); end
include TransferringMoney
end
# Единственное мое добавление в исходный класс - включение модуля TransferringMoney,
# это то, что DHH называет mixin on models, Account - модель в данном случае. Этот
# модуль будет в отдельном файле.
module TransferringMoney
def transfer!(destination_account_id, amount)
destination_account = Account.find(destination_account_id)
raise DestinationAccountNotFound unless destination_account
raise InsufficientFunds if balance < amount
update_log("Starting transferring from #{self.id} to #{destination_account_id}", amount)
decrease_balance(amount)
update_log("Balance of #{self.id} descreased", amount)
destination_account.increase(amount)
update_log("Balance of #{destination_account_id} increased", amount)
end
end
# Все эти update_log имитируют транзакции, чтобы если в процессе работы функции
# перезагрузится сервер у нас были точки в логе, чтобы восстановить ход событий,
# кому мы что уже успели перечислить и с кого что мы успели снять.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment