lib/balboa/cli/command/punch_command.rb in balboa-0.1.2 vs lib/balboa/cli/command/punch_command.rb in balboa-0.1.3
- old
+ new
@@ -1,43 +1,88 @@
# frozen_string_literal: true
+require 'holidays'
+require 'highline/import'
+
module Balboa
module CLI
module Command
class PunchCommand
- def initialize(interactor, holidays)
+ def initialize(interactor)
@interactor = interactor
- @holidays = holidays.uniq.map { |holiday| Date.parse(holiday) }
end
def execute
punch_dates.each do |date|
- $stdout.print(date.strftime("%d/%m/%Y"))
+ $stdout.print("\n#{date.strftime("%d/%m/%Y")}")
- if date.saturday? || date.sunday? || holiday?(date)
- $stdout.print(" # SKIPPED")
- else
- @interactor.punch(date)
- end
-
- $stdout.print("\n")
+ @interactor.punch(date) unless skip_date?(date)
end
end
private
def punch_dates
yesterday = Date.today - 1
- (last_punch_date..yesterday)
+ (last_punch_date..yesterday).map do |date|
+ PunchDate.new(date)
+ end
end
- def holiday?(date)
- @holidays.include?(date)
+ def skip_date?(date)
+ !date.punchable? || skips_include?(date)
end
+ def skips_include?(date)
+ @interactor.options['skips'].include?(date)
+ end
+
def last_punch_date
Date.parse(@interactor.last) + 1
+ end
+
+ class PunchDate
+ def initialize(date)
+ @date = date
+ @holiday = Hash(Holidays.on(@date, :br).first)
+ end
+
+ def punchable?
+ if weekend?
+ false
+ elsif holiday?
+ ask_for_punch
+ else
+ true
+ end
+ end
+
+ def to_s
+ @date.to_s
+ end
+
+ def strftime(format)
+ @date.strftime(format)
+ end
+
+ private
+
+ def ask_for_punch
+ HighLine.agree(" #{holiday_name}. Punch? (y|n) ")
+ end
+
+ def holiday_name
+ @holiday[:name]
+ end
+
+ def holiday?
+ !!@holiday
+ end
+
+ def weekend?
+ @date.sunday? || @date.saturday?
+ end
end
end
end
end
end