Skip to content

Instantly share code, notes, and snippets.

@devyn
Created November 14, 2008 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devyn/24745 to your computer and use it in GitHub Desktop.
Save devyn/24745 to your computer and use it in GitHub Desktop.
# DoughTracker, a GUI for creating goals of money.
require 'yaml'
class ::DTGoal
attr_accessor :name, :goal, :so_far
def initialize(name, goal)
@name, @goal = name, goal
@so_far = 0.0
end
def fraction
@so_far/@goal
end
def insert(amount)
@so_far += amount
end
def take_out(amount)
@so_far -= amount
end
end
class ::DTGoals
def initialize(filename)
@file_name = filename
@db = ((YAML.load_file(filename) rescue nil) or [])
end
def each(&blk)
@db.each &blk
end
def each_with_index(&blk)
@db.each_with_index &blk
end
def [](idx)
@db[idx]
end
def new_goal(*args)
@db << DTGoal.new(*args)
end
def delete_at(idx)
@db.delete_at idx
end
def save
File.open(@file_name, 'w') {|f| f.write YAML.dump(@db) }
end
end
class DoughTracker < Shoes
url '/', :index
url '/new_goal', :new_goal
url '/edit_goal/(\d+)', :edit_goal
def index
background lime
stack do
$DT_GOALS.each_with_index do |goal, idx|
stack { flow { para goal.name, :weight => 'bold'; para " need to raise $#{goal.goal}, have $#{goal.so_far}" }; flow { button('remove'){$DT_GOALS.delete_at idx; $DT_GOALS.save; visit '/'}; button('edit'){visit "/edit_goal/#{idx}"}; el=edit_line(:width=>100); button("deposit"){goal.insert(el.text.to_f);$DT_GOALS.save;visit '/'}; button("subtract"){goal.take_out(el.text.to_f);$DT_GOALS.save;visit '/'} }; flow { p=progress(:width => 1.0);timer(0.3){p.fraction=goal.fraction} }; para "" }
end
button("new goal") { visit '/new_goal' }
end
end
def new_goal
background green
nm,gl=nil # initialize the variables
stack do
flow {
para "Name: "
nm = edit_line(:width => 300)
}
flow {
para "Goal: "
gl = edit_line(:width => 100)
}
flow {
button "Create" do
$DT_GOALS.new_goal nm.text, gl.text.to_f
$DT_GOALS.save
visit '/'
end
button "Back" do
visit '/'
end
}
end
end
def edit_goal(idx)
g = $DT_GOALS[idx.to_i]
background green
nm,gl=nil #initialize the variables
stack do
flow {
para "Name: "
nm = edit_line(g.name, :width => 300)
}
flow {
para "Goal: "
gl = edit_line(g.goal, :width => 100)
}
flow {
button "Edit" do
g.name = nm.text
g.goal = gl.text.to_f
$DT_GOALS.save
visit '/'
end
button "Back" do
visit '/'
end
}
end
end
end
$DT_GOALS = ::DTGoals.new("dt.cfg.yml")
Shoes.app :title => "DoughTracker"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment