Class: Chronic::RepeaterFortnight

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

Overview

:nodoc:

Constant Summary collapse

FORTNIGHT_SECONDS =

(14 * 24 * 60 * 60)

1_209_600

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) ⇒ RepeaterFortnight

Returns a new instance of RepeaterFortnight.



4
5
6
7
# File 'lib/chronic/repeaters/repeater_fortnight.rb', line 4

def initialize(type)
  super
  @current_fortnight_start = nil
end

Instance Method Details

#next(pointer) ⇒ Object



9
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_fortnight.rb', line 9

def next(pointer)
  super
  
  if !@current_fortnight_start
    case pointer
    when :future
      sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
      sunday_repeater.start = @now
      next_sunday_span = sunday_repeater.next(:future)
      @current_fortnight_start = next_sunday_span.begin
    when :past
      sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
      sunday_repeater.start = (@now + Chronic::RepeaterDay::DAY_SECONDS)
      2.times { sunday_repeater.next(:past) }
      last_sunday_span = sunday_repeater.next(:past)
      @current_fortnight_start = last_sunday_span.begin
    end
  else
    direction = pointer == :future ? 1 : -1
    @current_fortnight_start += direction * FORTNIGHT_SECONDS
  end
  
  Chronic::Span.new(@current_fortnight_start, @current_fortnight_start + FORTNIGHT_SECONDS)
end

#offset(span, amount, pointer) ⇒ Object



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

def offset(span, amount, pointer)
  direction = pointer == :future ? 1 : -1
  span + direction * amount * FORTNIGHT_SECONDS
end

#this(pointer = :future) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chronic/repeaters/repeater_fortnight.rb', line 34

def this(pointer = :future)
  super
  
  pointer = :future if pointer == :none
  
  case pointer
  when :future
    this_fortnight_start = Time.construct(@now.year, @now.month, @now.day, @now.hour) + Chronic::RepeaterHour::HOUR_SECONDS
    sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
    sunday_repeater.start = @now
    sunday_repeater.this(:future)
    this_sunday_span = sunday_repeater.this(:future)
    this_fortnight_end = this_sunday_span.begin
    Chronic::Span.new(this_fortnight_start, this_fortnight_end)
  when :past
    this_fortnight_end = Time.construct(@now.year, @now.month, @now.day, @now.hour)
    sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
    sunday_repeater.start = @now
    last_sunday_span = sunday_repeater.next(:past)
    this_fortnight_start = last_sunday_span.begin
    Chronic::Span.new(this_fortnight_start, this_fortnight_end)
  end
end

#to_sObject



67
68
69
# File 'lib/chronic/repeaters/repeater_fortnight.rb', line 67

def to_s
  super << '-fortnight'
end

#widthObject



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

def width
  FORTNIGHT_SECONDS
end