Sha256: 96918c2c6d9918eb923cc2a6487f72356b104bd093611c06d55a8016ae816863

Contents?: true

Size: 1.39 KB

Versions: 10

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true
require_relative './support/unit'

module TimeBoss
  class Calendar
    class Week < Support::Unit
      def initialize(calendar, start_date, end_date)
        raise UnsupportedUnitError unless calendar.supports_weeks?
        super(calendar, start_date, end_date)
      end

      # Get a simple representation of this week.
      # @return [String] (e.g. "2020W32")
      def name
        "#{year_index}W#{index}"
      end

      # Get a "pretty" representation of this week.
      # @return [String] (e.g. "Week of August 3, 2020")
      def title
        "Week of #{start_date.strftime('%B %-d, %Y')}"
      end

      # Get a stringified representation of this week.
      # @return [String] (e.g. "2020W32: 2020-08-03 thru 2020-08-09")
      def to_s
        "#{name}: #{start_date} thru #{end_date}"
      end

      # Get the index of this week within its containing year.
      # @return [Integer]
      def index
        @_index ||= (((start_date - year.start_date) + 1) / 7.0).to_i + 1
      end

      # Get the year number for this week.
      # @return [Integer] (e.g. 2020)
      def year_index
        @_year_index ||= year.year_index
      end

      private

      def down
        self.class.new(calendar, start_date - 1.week, end_date - 1.week)
      end

      def up
        self.class.new(calendar, start_date + 1.week, end_date + 1.week)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
timeboss-1.0.1 lib/timeboss/calendar/week.rb
timeboss-1.0.0 lib/timeboss/calendar/week.rb
timeboss-0.3.1 lib/timeboss/calendar/week.rb
timeboss-0.3.0 lib/timeboss/calendar/week.rb
timeboss-0.2.5 lib/timeboss/calendar/week.rb
timeboss-0.2.4 lib/timeboss/calendar/week.rb
timeboss-0.2.3 lib/timeboss/calendar/week.rb
timeboss-0.2.2 lib/timeboss/calendar/week.rb
timeboss-0.2.1 lib/timeboss/calendar/week.rb
timeboss-0.2.0 lib/timeboss/calendar/week.rb