Figure 7-3. PayPal expense report output
Now, let??™s examine the code in more detail.
Dissecting the Code
The first part of the code in Listing 7-2 connects to the database and creates a model for
your transaction table.Most of the code in this book uses hard-coded values, but that??™s
CHAPTER 7 n TRACKING EXPENDITURES WITH PAYPAL 148
not necessary. This particular listing lets you specify your connection details on the command
line:
(puts "usage: #{$0} mysql_hostname mysql_username " <<
"mysql_password database_name"; exit) unless ARGV.length==4
mysql_hostname,
mysql_username,
mysql_password,
mysql_database= *ARGV
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => mysql_hostname,
:username => mysql_username,
:password => mysql_password,
:database => mysql_database) # Establish a connection to the database.
First, the line checks if the proper number of arguments have been passed. If not, it
tells the user what the program expects and exits. It uses the construct *ARGV to take the
four arguments and put them in four separate variables.
Pages:
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217