O'Reilly logo

The Cucumber Book, 2nd Edition by Steve Tooke, Aslak Hellesoy, Matt Wynne

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required