Sha256: e4e0792a3ede95d6991a782d6e19524b2ed77935d167a2a4824c19e3f7722005

Contents?: true

Size: 1.97 KB

Versions: 11

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true
module TimeBoss
  class Calendar
    module Support
      module Navigable
        # @overload previous
        #   Fetch the previous unit relative to this unit.
        #   @return [Unit]
        # @overload previous(value)
        #   Fetch some previous number of units relative to this unit
        #   @param quantity [Integer]
        #   @return [Array<Unit>]
        def previous(quantity = nil)
          return down if quantity.nil?
          gather(:previous, quantity).reverse
        end

        # @overload next
        #   Fetch the next unit relative to this unit.
        #   @return [Unit]
        # @overload next(value)
        #   Fetch some next number of units relative to this unit
        #   @param quantity [Integer]
        #   @return [Array<Unit>]
        def next(quantity = nil)
          return up if quantity.nil?
          gather(:next, quantity)
        end

        # Fetch the unit some number of units prior to this unit.
        # @param quantity [Integer]
        # @return [Unit]
        def ago(quantity)
          previous(quantity + 1).first
        end

        # Fetch the unit some number of units after this unit.
        # @param quantity [Integer]
        # @return [Unit]
        def ahead(quantity)
          self.next(quantity + 1).last
        end

        # Fetch a list of units from this unit until some date.
        # @param end_date [Date]
        # @return [Array<Unit>]
        def until(end_date)
          entry = self
          [entry].tap do |entries|
            until entry.end_date >= end_date
              entry = entry.next
              entries << entry
            end
          end
        end

        private

        def gather(navigator, quantity)
          [].tap do |entries|
            entry = self
            while quantity > 0
              entries << entry
              entry = entry.send(navigator)
              quantity -= 1
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

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