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:
databases/00/lib/account.rb | |
| require 'active_record' |
| ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', |
| :database => 'db/bank.db') |
| ActiveRecord::Migrator.migrate("db/migrate/") |
| class Account < ActiveRecord::Base |
| validates_uniqueness_of :number |
| def queue |
| @queue ||= TransactionQueue.new |
| end |
| def credit(amount) ... |
Get The Cucumber Book now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.