lib/time_cop/runner.rb in time_cop-0.6.0 vs lib/time_cop/runner.rb in time_cop-0.7.0

- old
+ new

@@ -9,66 +9,80 @@ options = options_parser.parse interactive_hash = {} if (options[:interactive]) cli = HighLine.new - username = cli.ask('Harvest Username: ') - password = cli.ask('Harvest Password: ') { |q| q.echo = false } - interactive_hash[:hours_per_week] = cli.ask('Hours per week? (Full Time 34)') { |q| q.default = 34 }.to_f + username = ENV['HARVEST_USERNAME'].nil? ? cli.ask('Harvest Username: ') : ENV['HARVEST_USERNAME'] + password = ENV['HARVEST_PASSWORD'].nil? ? cli.ask('Harvest Password: ') { |q| q.echo = false } : ENV['HARVEST_PASSWORD'] + interactive_hash[:hours_per_week] = ENV['HARVEST_HOURS_PER_WEEK'].nil? ? cli.ask('Hours per week? Defaults to Full Time (34)') { |q| q.default = 34 }.to_f : ENV['HARVEST_HOURS_PER_WEEK'].to_f + year = cli.ask("Year? Defaults to #{Date.today.year}") { |q| q.default = Date.today.year }.to_i accountability_options = { username: (username.nil? ? options[:username] : username), password: (password.nil? ? options[:password] : password), email: options[:email] } q_dates = [ - Date.new(Date.today.year, 1, 1), - Date.new(Date.today.year, 4, 1), - Date.new(Date.today.year, 7, 1), - Date.new(Date.today.year, 10, 1) + { + range: Date.new(year, 1, 1)...Date.new(year, 3, 31), + choice: :Q1 + }, + { + range: Date.new(year, 4, 1)...Date.new(year, 6, 30), + choice: :Q2 + }, + { + range: Date.new(year, 7, 1)...Date.new(year, 9, 30), + choice: :Q3 + }, + { + range: Date.new(year, 10, 1)...Date.new(year, 12, 31), + choice: :Q4 + } ] - + default_quarter = q_dates.find{|q_date| q_date[:range].include?(Date.today)} + default_choice = default_quarter.nil? ? '' : "Defaults to #{default_quarter[:choice]}" cli.choose do |menu| - menu.prompt = 'Which Quarter? ' - menu.choice(:Q1) do - interactive_hash[:date] = q_dates[0] - Accountability.new(interactive_hash.merge(accountability_options)).print_report - end - menu.choice(:Q2) do - interactive_hash[:date] = q_dates[1] - Accountability.new(interactive_hash.merge(accountability_options)).print_report + menu.prompt = "Which Quarter? #{default_choice}" + unless default_quarter.nil? + menu.default = default_quarter[:choice] end - menu.choice(:Q3) do - interactive_hash[:date] = q_dates[2] - Accountability.new(interactive_hash.merge(accountability_options)).print_report + q_dates.each do |q_date| + menu.choice(q_date[:choice]) do + interactive_hash[:date] = q_date[:range].begin + puts 'Fetching data...' + Accountability.new(interactive_hash.merge(accountability_options)).print_report + end end - menu.choice(:Q4) do - interactive_hash[:date] = q_dates[3] - Accountability.new(interactive_hash.merge(accountability_options)).print_report - end menu.choice(:Year) do - summary = q_dates.map do |d| - interactive_hash[:date] = d + summary = q_dates.map do |q_date| + interactive_hash[:date] = q_date[:range].begin + puts "Fetching data for #{q_date[:choice]}..." Accountability.new(interactive_hash.merge(accountability_options)).summary_hash end logged = summary.inject(0){|sum, s| sum + s[:hours][:charged]} needed = summary.inject(0){|sum, s| sum + s[:hours][:needed]}.round(2 ) diff = (logged - needed).round(2) - weekdays = (Date.today..Date.new(Date.today.year, 12, 31)).select{|d| (1..5).include?(d.wday)}.size - weekdays_so_far = (Date.new(Date.today.year, 1, 1)..Date.today).select{|d| (1..5).include?(d.wday)}.size + weekdays = (Date.today..Date.new(year, 12, 31)).select{|d| (1..5).include?(d.wday)}.size + weekdays_so_far = (Date.new(year, 1, 1)..Date.today).select{|d| (1..5).include?(d.wday)}.size average_needed = (-1 * diff / weekdays).round(2) average_clocked = (logged / weekdays_so_far).round(2) projected_diff = ((logged + (weekdays * average_clocked)) - needed).round(2) - - puts "Business Days Left In Year: #{weekdays}" - puts "Current Year End Surplus(+)/Deficit(-): #{diff}" - puts "Projected Year End Surplus(+)/Deficit(-): #{projected_diff}" - puts "" - puts "Total Harvest Hours This Year: #{logged}" + if (year == Date.today.year) + puts "Business Days Left In Year: #{weekdays}" + puts "Current Year End Surplus(+)/Deficit(-): #{diff}" + puts "Projected Year End Surplus(+)/Deficit(-): #{projected_diff}" + puts "" + else + puts "#{year} Surplus(+)/Deficit(-): #{diff}" + end + puts "Total Harvest Hours for #{year}: #{logged}" puts "Average Harvest Hours Per Business Day: #{average_clocked}" - puts "" - puts "Total Hours Needed By End Of Year: #{needed}" - puts "Average Hours Per Business Day Needed To Reach Goal: #{average_needed}" - end + if (year == Date.today.year) + puts "" + puts "Total Hours Needed By End Of Year: #{needed}" + puts "Average Hours Per Business Day Needed To Reach Goal: #{average_needed}" + end + end end else accountability_options = { username: (options[:username]), password: (options[:password]),