class SplitBills
require 'money' if I get to supporting currency
Public Class Methods
hi()
click to toggle source
# File lib/split_bills.rb, line 338 def self.hi puts "SplitBills, hello!" end
new()
click to toggle source
attr_accessor :members, :new_bill
# File lib/split_bills.rb, line 9 def initialize $members = Hash.new $bills = Hash.new $monthly_bills = Hash.new #$monthly_split = Hash.new @save_path = "./data/" @bills_file = "bills.yml" @members_file = "members.yml" date = Time.new @month_year = "#{date.year}_#{date.month}" @dated_split_file = "#{@month_year}_split.yml" @monthly_bills_file = "#{@month_year}_bills.yml" @divide_key = 0 @divide_by = Hash[:empty => 0] @mem_with_key = Array.new @split_bill_list = Array.new end
Public Instance Methods
alnum_chk(string)
click to toggle source
# File lib/split_bills.rb, line 301 def alnum_chk(string) string.index(/[^[:alnum:]]/) end
assign_bill_to_mem(mem_key, bill_key, value)
click to toggle source
# File lib/split_bills.rb, line 114 def assign_bill_to_mem(mem_key, bill_key, value) #broken work_mem = $members[mem_key] work_bill = $bills[bill_key] work_merge = work_mem.merge(work_bill) $members[mem_key] = work_merge end
assign_bill_ui()
click to toggle source
# File lib/split_bills.rb, line 111 def assign_bill_ui #ui element end
calculate_and_set_split(bill_key)
click to toggle source
# File lib/split_bills.rb, line 200 def calculate_and_set_split(bill_key) # [:rent] # for each of these set split total and set in members bills sub_key = return_sub_sym(bill_key) mem_sub_key = return_mem_sub_sym(bill_key) bill_total = $monthly_bills[bill_key][sub_key][1] elig_mem = get_eligible_member_list(bill_key) div_by = elig_mem.count split = bill_total.to_f/div_by.to_f elig_mem.each { |mem_key| set_mem_split(mem_key, mem_sub_key, split) } end
calculate_monthly_split()
click to toggle source
# File lib/split_bills.rb, line 169 def calculate_monthly_split # for each bill in monthly # get bills which are "split" # for each split bill # get members who owe # calculate number to split between # calculate what split equals # assign split value to member[:key][:subkey] # split_key_list = list_keys.each { |key| get_split_bill_list(key) } #[:rent], [:garbage] split_key_list.each { |key| get_split_bill_list(key) } @split_bill_list = @split_bill_list.uniq @split_bill_list.each { |key| calculate_and_set_split(key) } end
check_split(bill_key, mem_key)
click to toggle source
# File lib/split_bills.rb, line 236 def check_split(bill_key, mem_key) sub_key = return_mem_sub_sym(bill_key) if $bills[bill_key][sub_key][1].to_f > $members[mem_key][sub_key][1] true else false end end
check_time()
click to toggle source
# File lib/split_bills.rb, line 297 def check_time @month_year end
currency_check(string)
click to toggle source
# File lib/split_bills.rb, line 305 def currency_check(string) string.index(/[^\d]/) #string.index(/[^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$]/) #if I get to supporting currency end
del_member!(del_mem)
click to toggle source
# File lib/split_bills.rb, line 69 def del_member!(del_mem) $members.delete_if{ |m| m==del_mem } end
eligible_member_test(mem_key, sub_key)
click to toggle source
# File lib/split_bills.rb, line 219 def eligible_member_test(mem_key, sub_key) # [:scott_dodson], [:my_garbage] elig_list = Array.new #sub_key = return_mem_sub_sym(key) # [:my_garbage] #puts "#{key}, #{sub_key}" #key work_test = $members[mem_key][sub_key][0] if work_test=="split" elig_list << mem_key else end elig_list end
enter_bill()
click to toggle source
Bill methods
# File lib/split_bills.rb, line 75 def enter_bill #ui element name = gets_from_user("Bill name") split = gets_from_user("#{name} is split evenly or a fixed ammount (split/fixed)") value = gets_currency_from_user("if this is a fixed bill enter the ammount in whole numbers, otherwise enter 0") new_bill(name, split, value) end
enter_monthly_bill(bill_key, value)
click to toggle source
# File lib/split_bills.rb, line 137 def enter_monthly_bill(bill_key, value) #create monthly instance of bill and set total #get record from $bills #record_work = $bills[bill_key] # @divide_by = Hash[:empty => 0] # reset divide by count, used in splitting bills key_work = "mon_" + bill_key.to_s old_key = "my_" + bill_key.to_s old_key = old_key.to_sym new_key = key_work.to_sym copy_val = $bills[bill_key][old_key][0] $monthly_bills[bill_key] = { new_key =>[copy_val, value] } end
get_eligible_member_list(bill_key)
click to toggle source
# File lib/split_bills.rb, line 212 def get_eligible_member_list(bill_key) #eligible_members = Array.new mem_keys = $members.keys mem_sub_key = return_mem_sub_sym(bill_key) mem_keys.each { |mem_key| eligible_member_test(mem_key, mem_sub_key) } end
get_new_member()
click to toggle source
Member methods - elements are (member_name), (member_unit), (…)
# File lib/split_bills.rb, line 46 def get_new_member # transfer to UI class/module first_name = gets_from_user("Members first name") last_name = gets_from_user("Members last name") new_member(first_name, last_name) end
get_split_bill_list(key)
click to toggle source
# File lib/split_bills.rb, line 184 def get_split_bill_list(key) #evaluate sub_keys for instance of "split" and store key = :rent, sub_key = :mon_rent # for each bill in monthly # get bills which are "split" # evaluate sub_key[0] for "split" # save results into an array # #split_list = Array.new #$monthly_bills[key].each {||} sub_sym = return_sub_sym(key) # [:mon_garbage] if $monthly_bills[key][sub_sym][0]=="split" @split_bill_list << key else end @split_bill_list end
gets_currency_from_user(question)
click to toggle source
# File lib/split_bills.rb, line 320 def gets_currency_from_user(question) puts "#{question}:" user_in = gets.chomp if currency_check(user_in) != nil puts "no special characters please:" else user_in end end
gets_from_user(question)
click to toggle source
# File lib/split_bills.rb, line 310 def gets_from_user(question) puts "#{question}:" user_in = gets.chomp if alnum_chk(user_in) != nil puts "no special characters please:" else user_in end end
list_bills()
click to toggle source
# File lib/split_bills.rb, line 103 def list_bills $bills.each { |key, value| puts "#{key}" } end
list_keys()
click to toggle source
# File lib/split_bills.rb, line 165 def list_keys $monthly_bills.keys end
list_members_bills(mem_key)
click to toggle source
# File lib/split_bills.rb, line 107 def list_members_bills(mem_key) #working on this $members[mem_key].each { |key, value| puts "#{sym_to_name(key)}" } end
load_all()
click to toggle source
Startup shortcuts
# File lib/split_bills.rb, line 29 def load_all load_members load_bills load_monthly_bills #load_monthly_split #populate_divide_by end
load_bills()
click to toggle source
# File lib/split_bills.rb, line 93 def load_bills $bills = load_file(@bills_file, $bills) #load_work = load_file(@bills_file, $bills) #$bills = load_work end
load_file(file, data)
click to toggle source
# File lib/split_bills.rb, line 285 def load_file(file, data) if File.exists?("#{@save_path + file}") data = YAML.load_file("#{@save_path + file}") else data = File.new("#{@save_path + file}", "w") end end
load_members()
click to toggle source
# File lib/split_bills.rb, line 63 def load_members $members = load_file(@members_file, $members) #load_work = load_file(@members_file, $members) #$members = load_work end
load_monthly_bills()
click to toggle source
# File lib/split_bills.rb, line 131 def load_monthly_bills $monthly_bills = load_file(@monthly_bills_file, $monthly_bills) #load_work = load_file(@dated_bills_file, $monthly_bills) #$monthly_bills = load_work end
load_monthly_split()
click to toggle source
# File lib/split_bills.rb, line 159 def load_monthly_split $monthly_split = load_file(@dated_split_file, $monthly_split) #load_work = load_file(@dated_split_file, $monthly_split) #$monthly_split = load_work end
name_to_sym(sym_name)
click to toggle source
# File lib/split_bills.rb, line 277 def name_to_sym(sym_name) #convert name to symbol sym_name.gsub(/\b\w/){|f| f.downcase}.gsub(' ', '_').to_sym end
new_bill(name, split, value)
click to toggle source
# File lib/split_bills.rb, line 82 def new_bill(name, split, value) bill_sym = name_to_sym(name) bill_name = "my_" + name.downcase bill_name_sym = name_to_sym(bill_name) $bills[bill_sym] = { bill_name_sym =>[split, value] } end
new_member(first_name, last_name)
click to toggle source
# File lib/split_bills.rb, line 52 def new_member(first_name, last_name) new_mem = [first_name, last_name].reject(&:empty?).join(' ') new_mem = name_to_sym(new_mem) $members[new_mem] = { :my_bills =>["total", 0] } end
remove_bill(del_bill)
click to toggle source
# File lib/split_bills.rb, line 99 def remove_bill(del_bill) $bills.delete_if{ |b| b==del_bill } end
remove_bill_from_mem(mem_key, bill_key)
click to toggle source
# File lib/split_bills.rb, line 121 def remove_bill_from_mem(mem_key, bill_key) $members[mem_key] = $members[mem_key].delete_if{ |m| m==bill_key } end
remove_bill_from_mon(del_bill)
click to toggle source
# File lib/split_bills.rb, line 149 def remove_bill_from_mon(del_bill) $monthly_bills.delete_if{ |b| b==del_bill } end
return_mem_sub_sym(sym)
click to toggle source
# File lib/split_bills.rb, line 334 def return_mem_sub_sym(sym) #for getting number to split by ("my_" + sym.to_s).to_sym end
return_sub_sym(sym)
click to toggle source
# File lib/split_bills.rb, line 330 def return_sub_sym(sym) ("mon_" + sym.to_s).to_sym end
save_all()
click to toggle source
# File lib/split_bills.rb, line 37 def save_all save_members save_bills save_monthly_bills #save_monthly_split end
save_bills()
click to toggle source
# File lib/split_bills.rb, line 89 def save_bills save_file(@bills_file, $bills) end
save_file(file, data)
click to toggle source
# File lib/split_bills.rb, line 281 def save_file(file, data) File.open("#{@save_path}#{file}", "w"){ |f| f.write data.to_yaml } end
save_members()
click to toggle source
# File lib/split_bills.rb, line 58 def save_members #File.open("./data/members.yml", "w"){ |f| f.write @members.to_yaml } save_file(@members_file, $members) end
save_monthly_bills()
click to toggle source
Monthly bills methods
# File lib/split_bills.rb, line 127 def save_monthly_bills save_file(@monthly_bills_file, $monthly_bills) end
save_monthly_split()
click to toggle source
Monthly split methods - Working
# File lib/split_bills.rb, line 155 def save_monthly_split save_file(@dated_split_file, $monthly_split) end
set_mem_split(mem_key, mem_sub_key, split)
click to toggle source
# File lib/split_bills.rb, line 232 def set_mem_split(mem_key, mem_sub_key, split) $members[mem_key][mem_sub_key][1] = split end
set_save_path(path)
click to toggle source
# File lib/split_bills.rb, line 293 def set_save_path(path) @save_path = path end
show_bills()
click to toggle source
# File lib/split_bills.rb, line 255 def show_bills $bills end
show_divide_by()
click to toggle source
Test Methods
# File lib/split_bills.rb, line 247 def show_divide_by @divide_by end
show_mem_with_key()
click to toggle source
# File lib/split_bills.rb, line 251 def show_mem_with_key @mem_with_key end
show_members()
click to toggle source
# File lib/split_bills.rb, line 263 def show_members $members end
show_members_bills(mem_key)
click to toggle source
# File lib/split_bills.rb, line 267 def show_members_bills(mem_key) $members[mem_key] end
show_monthly_bills()
click to toggle source
# File lib/split_bills.rb, line 259 def show_monthly_bills $monthly_bills end
sym_to_name(name_sym)
click to toggle source
Class work methods
# File lib/split_bills.rb, line 273 def sym_to_name(name_sym) #convert symbol to name name_sym.to_s.gsub!('_', ' ').gsub(/\b\w/){|f| f.upcase} end