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:

 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 ...

Get The Cucumber Book, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.