Sha256: e4ffdb47029b499093cc2a3c1424b3fb0333930dece7cd53cc36954aa8ded6d8

Contents?: true

Size: 1.98 KB

Versions: 32

Compression:

Stored size: 1.98 KB

Contents

module Chronic
  class RepeaterWeekend < Repeater #:nodoc:
    WEEKEND_SECONDS = 172_800 # (2 * 24 * 60 * 60)

    def initialize(type)
      super
    end

    def next(pointer)
      super

      if !@current_week_start
        case pointer
        when :future
          saturday_repeater = RepeaterDayName.new(:saturday)
          saturday_repeater.start = @now
          next_saturday_span = saturday_repeater.next(:future)
          @current_week_start = next_saturday_span.begin
        when :past
          saturday_repeater = RepeaterDayName.new(:saturday)
          saturday_repeater.start = (@now + RepeaterDay::DAY_SECONDS)
          last_saturday_span = saturday_repeater.next(:past)
          @current_week_start = last_saturday_span.begin
        end
      else
        direction = pointer == :future ? 1 : -1
        @current_week_start += direction * RepeaterWeek::WEEK_SECONDS
      end

      Span.new(@current_week_start, @current_week_start + WEEKEND_SECONDS)
    end

    def this(pointer = :future)
      super

      case pointer
      when :future, :none
        saturday_repeater = RepeaterDayName.new(:saturday)
        saturday_repeater.start = @now
        this_saturday_span = saturday_repeater.this(:future)
        Span.new(this_saturday_span.begin, this_saturday_span.begin + WEEKEND_SECONDS)
      when :past
        saturday_repeater = RepeaterDayName.new(:saturday)
        saturday_repeater.start = @now
        last_saturday_span = saturday_repeater.this(:past)
        Span.new(last_saturday_span.begin, last_saturday_span.begin + WEEKEND_SECONDS)
      end
    end

    def offset(span, amount, pointer)
      direction = pointer == :future ? 1 : -1
      weekend = RepeaterWeekend.new(:weekend)
      weekend.start = span.begin
      start = weekend.next(pointer).begin + (amount - 1) * direction * RepeaterWeek::WEEK_SECONDS
      Span.new(start, start + (span.end - span.begin))
    end

    def width
      WEEKEND_SECONDS
    end

    def to_s
      super << '-weekend'
    end
  end
end

Version data entries

32 entries across 32 versions & 5 rubygems

Version Path
chronic-mmlac-0.6.4.2 lib/chronic/repeaters/repeater_weekend.rb
chronic-0.9.1 lib/chronic/repeaters/repeater_weekend.rb
chronic-0.9.0 lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.1.5 chronic/lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.1.4 chronic/lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.1.3 chronic/lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.1.2 chronic/lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.1.1 chronic/lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.1.0 chronic/lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.0.9 chronic/lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.0.6 chronic/lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.0.5 chronic/lib/chronic/repeaters/repeater_weekend.rb
Hokkaido-0.0.4 chronic/lib/chronic/repeaters/repeater_weekend.rb
chronic-0.8.0 lib/chronic/repeaters/repeater_weekend.rb
chronic-0.7.0 lib/chronic/repeaters/repeater_weekend.rb
chronic_2001-0.1.5 lib/chronic/repeaters/repeater_weekend.rb
chronic_2001-0.1.4 lib/chronic/repeaters/repeater_weekend.rb
chronic_2001-0.1.3 lib/chronic/repeaters/repeater_weekend.rb
chronic_2001-0.1.2 lib/chronic/repeaters/repeater_weekend.rb
chronic_2001-0.1.1 lib/chronic/repeaters/repeater_weekend.rb