Skip to content

Instantly share code, notes, and snippets.

@falonofthetower
Created August 29, 2016 01:15
Show Gist options
  • Select an option

  • Save falonofthetower/5a5de7ade7c36a09a544133abc49054b to your computer and use it in GitHub Desktop.

Select an option

Save falonofthetower/5a5de7ade7c36a09a544133abc49054b to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require "pg"
def list_expenses
connection = PG.connect(dbname: "expenses")
result = connection.exec("SELECT * FROM expenses ORDER BY created_on ASC")
result.each do |tuple|
columns = [ tuple["id"].rjust(3),
tuple["created_on"].rjust(10),
tuple["amount"].rjust(12),
tuple["memo"] ]
puts columns.join(" | ")
end
end
def display_help
puts <<~HELP
An expense recording system
Commands:
add AMOUNT MEMO [DATE] - record a new expense
clear - delete all expenses
list - list all expenses
delete NUMBER - remove expense with id NUMBER
search QUERY - list expenses with a matching memo field
HELP
end
command = ARGV.first
if command == "list"
list_expenses
else
display_help
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment