Sha256: ec305df3be70f9524b0ac5083918f88a8d9b1831b1df65d0656c8bec4e343d46

Contents?: true

Size: 1.7 KB

Versions: 66

Compression:

Stored size: 1.7 KB

Contents

module PgEngine
  class DateJumper
    def initialize(start_date)
      @start_date = start_date
    end

    def business_days(days, direction:, exclude_holidays: false)
      case direction
      when :forward, 'forward'
        business_forward(days, exclude_holidays:)
      when :backward, 'backward'
        business_backward(days, exclude_holidays:)
      else
        # :nocov:
        raise PgEngine::Error, 'direction not supported'
        # :nocov:
      end
    end

    def business_forward(days, exclude_holidays: false)
      days.times.inject(@start_date) do |acc, _|
        if exclude_holidays
          find_excluding_holidays(method: :next_business_day, from: acc)
        else
          next_business_day(acc)
        end
      end
    end

    def business_backward(days, exclude_holidays: false)
      days.times.inject(@start_date) do |acc, _|
        if exclude_holidays
          find_excluding_holidays(method: :prev_business_day, from: acc)
        else
          prev_business_day(acc)
        end
      end
    end

    private

    def find_excluding_holidays(method:, from:)
      aux = from
      safe_counter = 0
      loop do
        safe_counter += 1
        aux = send(method, aux)
        if safe_counter > 10
          # :nocov:
          raise 'las cosas'
          # :nocov:
        end
        break unless Holidays.on(aux, :ar).any?
      end
      aux
    end

    def next_business_day(date)
      if date.wday.in? [0, 5, 6]
        date.next_occurring(:monday)
      else
        date.advance(days: 1)
      end
    end

    def prev_business_day(date)
      if date.wday.in? [0, 1, 6]
        date.prev_occurring(:friday)
      else
        date.advance(days: -1)
      end
    end
  end
end

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
pg_rails-7.6.27 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.26 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.25 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.24 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.24.pre.5 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.24.pre.4 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.24.pre.3 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.23 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.22 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.22.pre.3 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.22.pre.2 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.22.pre.1 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.21 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.21.pre.11 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.21.pre.10 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.21.pre.9 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.21.pre.8 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.21.pre.7 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.21.pre.6 pg_engine/app/lib/pg_engine/date_jumper.rb
pg_rails-7.6.21.pre.5 pg_engine/app/lib/pg_engine/date_jumper.rb