Sha256: 21b8b40db3993ddcef92f0595336e5e928ee0ea6ead906960f299ade25fbf624

Contents?: true

Size: 1.29 KB

Versions: 44

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Renalware
  class WeekPeriod
    attr_reader :year, :week_number, :first_day_of_week

    def self.from_date(date)
      date = date.to_date
      new(week_number: date.cweek, year: date.year)
    end

    def initialize(week_number:, year:)
      @week_number = week_number.to_i
      @year = year.to_i
      validate_week_number
      validate_year
      @first_day_of_week = Date.commercial(@year, @week_number)
    end

    def next
      self.class.from_date(first_day_of_week + 1.week)
    end

    def previous
      self.class.from_date(first_day_of_week - 1.week)
    end

    def to_a
      [week_number, year]
    end

    def to_s
      "#{I18n.l(first_day_of_week, format: :long)} to #{I18n.l(last_day_of_week, format: :long)}"
    end

    # The date of last day of the week (a Sunday)
    def last_day_of_week
      @last_day_of_week ||= (first_day_of_week + 1.week - 1.minute).to_date
    end

    def validate_week_number
      if week_number < 1 || week_number > 53
        raise(ArgumentError, "invalid date: week_number must be 1-53")
      end
    end

    def validate_year
      raise(ArgumentError, "invalid date year must be >= 2000") if year <= 2000
    end

    def to_h
      {
        year: year,
        week_number: week_number
      }
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
renalware-core-2.0.113 lib/renalware/week_period.rb
renalware-core-2.0.112 lib/renalware/week_period.rb
renalware-core-2.0.111 lib/renalware/week_period.rb
renalware-core-2.0.110 lib/renalware/week_period.rb
renalware-core-2.0.109 lib/renalware/week_period.rb
renalware-core-2.0.108 lib/renalware/week_period.rb
renalware-core-2.0.106 lib/renalware/week_period.rb
renalware-core-2.0.105 lib/renalware/week_period.rb
renalware-core-2.0.104 lib/renalware/week_period.rb
renalware-core-2.0.103 lib/renalware/week_period.rb
renalware-core-2.0.102 lib/renalware/week_period.rb
renalware-core-2.0.101 lib/renalware/week_period.rb
renalware-core-2.0.100 lib/renalware/week_period.rb
renalware-core-2.0.99 lib/renalware/week_period.rb
renalware-core-2.0.98 lib/renalware/week_period.rb
renalware-core-2.0.97 lib/renalware/week_period.rb
renalware-core-2.0.96 lib/renalware/week_period.rb
renalware-core-2.0.95 lib/renalware/week_period.rb
renalware-core-2.0.94 lib/renalware/week_period.rb
renalware-core-2.0.93 lib/renalware/week_period.rb