Sha256: abcc93783f3d7d3a3ef8c1056e7b5504860c5683d4e9a1519209b586cef4dbda

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require 'holidays'
require 'highline/import'

module Balboa
  module CLI
    module Command
      class PunchCommand
        def initialize(interactor)
          @interactor = interactor
        end

        def execute
          punch_dates.each do |date|
            $stdout.print("\n#{date.strftime("%d/%m/%Y")}")

            @interactor.punch(date) unless skip_date?(date)
          end
        end

        private

        def punch_dates
          yesterday = Date.today - 1

          (last_punch_date..yesterday).map do |date|
            PunchDate.new(date)
          end
        end

        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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
balboa-0.1.3 lib/balboa/cli/command/punch_command.rb