Sha256: 80011158d7082b50782455633650e42526fa4527e0911ad006e2bf1c79b7528a

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true
module TimeBoss
  class Calendar
    class Period
      attr_reader :begin, :end
      delegate :start_date, to: :begin
      delegate :end_date, to: :end

      %i[name title to_s].each do |message|
        define_method(message) do
          text = self.begin.send(message)
          text = "#{text} #{Parser::RANGE_DELIMITER} #{self.end.send(message)}" unless self.end == self.begin
          text
        end
      end

      %w[week month quarter half year].each do |size|
        define_method(size.pluralize) do
          entry = calendar.send("#{size}_for", self.begin.start_date)
          build_entries entry
        end

        define_method(size) do
          entries = send(size.pluralize)
          return nil unless entries.length == 1
          entries.first
        end
      end

      def current?
        to_range.include?(Date.today)
      end

      def days
        to_range.map { |d| Day.new(calendar, d) }
      end

      def day
        entries = days
        return nil unless entries.length == 1
        entries.first
      end

      def to_range
        @_to_range ||= start_date .. end_date
      end

      private

      attr_reader :calendar

      def initialize(calendar, begin_basis, end_basis = nil)
        @calendar = calendar
        @begin = begin_basis
        @end = end_basis || @begin
      end

      def build_entries(entry)
        return [] if entry.start_date > self.end.end_date
        entries = [entry]
        while entry.end_date < self.end.end_date
          entry = entry.next
          entries << entry
        end
        entries
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
timeboss-0.0.10 lib/timeboss/calendar/period.rb
timeboss-0.0.9 lib/timeboss/calendar/period.rb
timeboss-0.0.8 lib/timeboss/calendar/period.rb
timeboss-0.0.7 lib/timeboss/calendar/period.rb
timeboss-0.0.6 lib/timeboss/calendar/period.rb
timeboss-0.0.5 lib/timeboss/calendar/period.rb