Class: Chronic::RepeaterWeek

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

Overview

:nodoc:

Constant Summary collapse

WEEK_SECONDS =

(7 * 24 * 60 * 60)

604800

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 = {}) ⇒ RepeaterWeek

Returns a new instance of RepeaterWeek.


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

def initialize(type, options = {})
  super
  @repeater_day_name = options[:week_start] || :sunday
  @current_week_start = nil
end

Instance Method Details

#next(pointer) ⇒ Object


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

def next(pointer)
  super

  unless @current_week_start
    case pointer
    when :future
      first_week_day_repeater = RepeaterDayName.new(@repeater_day_name)
      first_week_day_repeater.start = @now
      next_span = first_week_day_repeater.next(:future)
      @current_week_start = next_span.begin
    when :past
      first_week_day_repeater = RepeaterDayName.new(@repeater_day_name)
      first_week_day_repeater.start = (@now + RepeaterDay::DAY_SECONDS)
      first_week_day_repeater.next(:past)
      last_span = first_week_day_repeater.next(:past)
      @current_week_start = last_span.begin
    end
  else
    direction = pointer == :future ? 1 : -1
    @current_week_start += direction * WEEK_SECONDS
  end

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

#offset(span, amount, pointer) ⇒ Object


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

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

#this(pointer = :future) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chronic/repeaters/repeater_week.rb', line 36

def this(pointer = :future)
  super

  case pointer
  when :future
    this_week_start = Chronic.time_class.local(@now.year, @now.month, @now.day, @now.hour) + RepeaterHour::HOUR_SECONDS
    first_week_day_repeater = RepeaterDayName.new(@repeater_day_name)
    first_week_day_repeater.start = @now
    this_span = first_week_day_repeater.this(:future)
    this_week_end = this_span.begin
    Span.new(this_week_start, this_week_end)
  when :past
    this_week_end = Chronic.time_class.local(@now.year, @now.month, @now.day, @now.hour)
    first_week_day_repeater = RepeaterDayName.new(@repeater_day_name)
    first_week_day_repeater.start = @now
    last_span = first_week_day_repeater.next(:past)
    this_week_start = last_span.begin
    Span.new(this_week_start, this_week_end)
  when :none
    first_week_day_repeater = RepeaterDayName.new(@repeater_day_name)
    first_week_day_repeater.start = @now
    last_span = first_week_day_repeater.next(:past)
    this_week_start = last_span.begin
    Span.new(this_week_start, this_week_start + WEEK_SECONDS)
  end
end

#to_sObject


72
73
74
# File 'lib/chronic/repeaters/repeater_week.rb', line 72

def to_s
  super << '-week'
end

#widthObject


68
69
70
# File 'lib/chronic/repeaters/repeater_week.rb', line 68

def width
  WEEK_SECONDS
end