Class: Chronic::RepeaterWeekend

Inherits:
Repeater show all
Defined in:
lib/chronic/repeaters/repeater_weekend.rb

Overview

:nodoc:

Constant Summary collapse

WEEKEND_SECONDS =

(2 * 24 * 60 * 60)

172_800

Instance Attribute Summary

Attributes inherited from Tag

#type

Instance Method Summary collapse

Methods inherited from Repeater

#<=>, scan, scan_for_day_names, scan_for_day_portions, scan_for_month_names, scan_for_season_names, scan_for_times, scan_for_units

Methods inherited from Tag

#start=

Constructor Details

#initialize(type, options = {}) ⇒ RepeaterWeekend

Returns a new instance of RepeaterWeekend.


5
6
7
8
# File 'lib/chronic/repeaters/repeater_weekend.rb', line 5

def initialize(type, options = {})
  super
  @current_week_start = nil
end

Instance Method Details

#next(pointer) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chronic/repeaters/repeater_weekend.rb', line 10

def next(pointer)
  super

  unless @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

#offset(span, amount, pointer) ⇒ Object


51
52
53
54
55
56
57
# File 'lib/chronic/repeaters/repeater_weekend.rb', line 51

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

#this(pointer = :future) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chronic/repeaters/repeater_weekend.rb', line 34

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

#to_sObject


63
64
65
# File 'lib/chronic/repeaters/repeater_weekend.rb', line 63

def to_s
  super << '-weekend'
end

#widthObject


59
60
61
# File 'lib/chronic/repeaters/repeater_weekend.rb', line 59

def width
  WEEKEND_SECONDS
end