How it Works
A Hashbang directs the program to the bash interpreter. An instance method of a CLI class accepts command line arguments from the user, stores them in the ARGV array, and initializes an instance of an ExpenseData class. The ExpenseData instance establishes a SQL database connection via the PG RubyGem, and sets up a table with the required schema. The user's arguments trigger database queries which are sanitized to prevent malicious injections. A private method extracts data from the resulting tuple object and displays the expenses.
- When the program is run without arguments, a list of potential commands is shown to the suer.
- The user can enter a string to search for expenses.
- If the user wants to clear all records, confirmation is required. The IO class and STDIN#getch allow for the confirmation to be gathered in a single keystroke.
- Visually formatting expense data for display was difficult due to the varying lengths of all table column names and the values involved. String#rjust and Kernel#sprintf were a big help.
- Working with tuple objects is confusing. Even though they are hash-like objects, ruby methods cannot be called on them.