Skip to content

Instantly share code, notes, and snippets.

@addisaden
Created March 10, 2014 23:20
Show Gist options
  • Select an option

  • Save addisaden/9476513 to your computer and use it in GitHub Desktop.

Select an option

Save addisaden/9476513 to your computer and use it in GitHub Desktop.
just created a quick todolist script with active record
require "active_record"
class Todo < ActiveRecord::Base
establish_connection adapter: 'sqlite3', database: 'sqlite3.quicktodo.db'
unless connection.table_exists?(table_name) then
connection.create_table table_name, force: true do |t|
t.boolean :done, default: false
t.string :todo
end
end
end
def list_all
puts
counted = 0
Todo.all.each do |t|
puts "#{ t.id }: #{ t.todo } (#{ t.done ? 'done' : 'todo' })"
counted += 1
end
puts " #{ counted } todos."
end
def choose_one
list_all
puts
print "Please select by number: "
eingabe = gets.strip.to_i
if eingabe > 0 then
return Todo.find(eingabe)
else
return nil
end
end
loop do
puts
puts "create, delete, done, undone, list, rename, exit"
print "choose: "
eingabe = gets.strip
if eingabe =~ /exit/mi then
puts "Goodbye"
break
elsif eingabe =~ /create/mi then
tmp = Todo.new
print "Todo: "
tmp.todo = gets.strip
if tmp.save then
puts "gespeichert"
else
puts "Fehler"
end
elsif eingabe =~ /delete/mi then
tmp = choose_one
if tmp then
tmp.destroy
end
elsif eingabe =~ /undone/mi then
tmp = choose_one
if tmp then
tmp.done = false
tmp.save
end
elsif eingabe =~ /done/mi then
tmp = choose_one
if tmp then
tmp.done = true
tmp.save
end
elsif eingabe =~ /list/mi then
list_all
elsif eingabe =~ /rename/mi then
tmp = choose_one
if tmp then
print "rename: "
tmp.todo = gets.strip
tmp.save
end
else
puts "error: Befehl unbekannt."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment