Created
September 11, 2012 13:33
-
-
Save maxnik/3698496 to your computer and use it in GitHub Desktop.
Data Context Interaction (DCI) пример с TransferringMoney
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Я решил оставить его исходный интерфейс для класса 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