# This class parses the hourly rate for each employee found in the csv. # The first time it ask the user what the price is and saved it to a yml file with all the information needed # This information is retrieved require 'yaml' class HourlyRate def initialize(csv) # Read users from file @config_file = "#{ENV['HOME']}/.twstats_users.yml" @users = csv.people @config = {} if File.exists? @config_file @config = YAML.load(File.read(@config_file)) end # Set value of 0 to users that do not exist csv.people.each do |user| if @config[user].nil? @config[user] = 0 end end end def get_price(user,hours) # Get a given price for a given user end def set_price(user,price) @config[user] = price.round(2) end def save file = File.new(@config_file,'w') file.write YAML.dump(@config) file.close end def get_billed_time(users_hours) # It expects a hash with the amount of hours billed per user users_hours.keys.inject(0) do |sum, user| sum + @config[user]*users_hours[user] end end def set_user_hourly_rate(prompt) menu = @users.map{ |user| ["#{user} (#{@config[user].round(2)} €)", user]}.to_h menu['Quit'] = -1 loop do chosen = prompt.select 'What user you want to modify? Select Quit to finish.', menu break if chosen == -1 price = prompt.ask "What is the hourly rate for #{chosen}?", convert: :float puts price, chosen @config[chosen] = price menu = @users.map{ |user| ["#{user} (#{@config[user].round(2)} €)", user]}.to_h menu['Quit'] = -1 end save end end