Refactoring to Use a Database
In our current system, the balance of the single account is stored in a file and read and written by the BalanceStore class. In our new design, we will make the Account responsible for reading the balance straight out of a database instead. It’s refactoring time again!
We’ll start by moving the Account class into its own lib/account.rb file and make it an ActiveRecord class:
|||ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',|
|||:database => 'db/bank.db')|
|||class Account < ActiveRecord::Base|
|||@queue ||= TransactionQueue.new|