Created
July 22, 2012 02:35
-
-
Save mxswd/3158003 to your computer and use it in GitHub Desktop.
My shopping list script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| :items: | |
| - :name: Drink Grapefruit | |
| :price: '4.59' | |
| :owner: max | |
| :paid: | |
| :max: 0 | |
| :ritwik: 314 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'yaml' | |
| require 'optparse' | |
| options = {} | |
| OptionParser.new do |opts| | |
| opts.banner = "Usage: shopping_list.rb [options]" | |
| opts.on("-i", "--input", "Input a shopping list") do |v| | |
| options[:input_mode] = v | |
| end | |
| end.parse! | |
| def load_yaml | |
| YAML::load(File.read("shopping.yml")) || {:items => []} rescue {:items => []} | |
| end | |
| def write_yaml(yaml) | |
| File.open("shopping.yml", 'w') {|f| f.write YAML::dump(yaml) } | |
| end | |
| if !options[:input_mode] | |
| require 'sinatra' | |
| def option_for(item, name) | |
| if item[:owner] == name | |
| "<option selected value=\"#{name}\">#{name}</option>" | |
| elsif item[:owner] == nil && name == "unknown" | |
| "<option selected value=\"#{name}\">#{name}</option>" | |
| else | |
| "<option value=\"#{name}\">#{name}</option>" | |
| end | |
| end | |
| get '/' do | |
| @items = load_yaml[:items] | |
| @paid = load_yaml[:paid] | |
| @total = {} | |
| @paid.each do |k, v| | |
| @total[k] = 0 | |
| end | |
| @items.each do |item| | |
| if item[:owner].to_sym == :both | |
| @total.each_key do |key| | |
| @total[key] += item[:price].to_f / @total.count | |
| end | |
| else | |
| @total[item[:owner].to_sym] += item[:price].to_f | |
| end | |
| end | |
| erb :index | |
| end | |
| post '/update/:id' do | |
| items = load_yaml[:items] | |
| items[params[:id].to_i][:owner] = params[:owner] | |
| write_yaml(items) | |
| redirect '/' | |
| end | |
| end | |
| if options[:input_mode] | |
| def append_yaml(item) | |
| yaml = load_yaml[:items] | |
| yaml << item | |
| File.open("shopping.yml", 'w') {|f| f.write YAML::dump(yaml) } | |
| end | |
| while true | |
| puts | |
| item = {} | |
| puts "Item Name: " | |
| item[:name] = gets.chomp | |
| puts "Price: " | |
| item[:price] = gets.chomp | |
| item[:owner] = nil | |
| append_yaml(item) | |
| end | |
| end | |
| __END__ | |
| @@ layout | |
| <html> | |
| <head> | |
| <title>Shopping List</title> | |
| </head> | |
| <body> | |
| <%= yield %> | |
| </body> | |
| </html> | |
| @@ index | |
| <h1>Owed</h1> | |
| <% @total.each do |k, v| %> | |
| <%= k %>'s Total: <%= v.round(2)%><br /> | |
| <% end %> | |
| <% @paid.each do |k, v| %> | |
| <%= k %>'s Paid: <%= v.round(2)%><br /> | |
| <% end %> | |
| <% @total.each do |k, v| %> | |
| <%= k %>'s Owes: <%= v.round(2) - @paid[k].round(2) %><br /> | |
| <% end %> | |
| <h1>Items!</h1> | |
| <ul> | |
| <% @items.each_with_index do |item, i| %> | |
| <li><dt><%= item[:name] %><dt><dd><%= item[:price] %> for <%= item[:owner] || "<span style=\"color: red\">Unknown</span>"%></dd> | |
| <form action="/update/<%= i %>" method="POST"> | |
| <select name="owner"> | |
| <% @paid.each do |k, v| %> | |
| <%= option_for(item, k) %> | |
| <% end %> | |
| <%= option_for(item, "both") %> | |
| <%= option_for(item, "unknown") %> | |
| </select> | |
| <input type="submit" value="Submit" /> | |
| </form> | |
| </li> | |
| <% end %> | |
| </ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment