Class: Chronic::RepeaterWeekday

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

Overview

:nodoc:

Constant Summary collapse

DAY_SECONDS =

(24 * 60 * 60)

86400
DAYS =
{
  :sunday => 0,
  :monday => 1,
  :tuesday => 2,
  :wednesday => 3,
  :thursday => 4,
  :friday => 5,
  :saturday => 6
}

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

Returns a new instance of RepeaterWeekday.



14
15
16
17
# File 'lib/chronic/repeaters/repeater_weekday.rb', line 14

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

Instance Method Details

#next(pointer) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chronic/repeaters/repeater_weekday.rb', line 19

def next(pointer)
  super

  direction = pointer == :future ? 1 : -1

  unless @current_weekday_start
    @current_weekday_start = Chronic.construct(@now.year, @now.month, @now.day)
    @current_weekday_start += direction * DAY_SECONDS

    until is_weekday?(@current_weekday_start.wday)
      @current_weekday_start += direction * DAY_SECONDS
    end
  else
    loop do
      @current_weekday_start += direction * DAY_SECONDS
      break if is_weekday?(@current_weekday_start.wday)
    end
  end

  Span.new(@current_weekday_start, @current_weekday_start + DAY_SECONDS)
end

#offset(span, amount, pointer) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chronic/repeaters/repeater_weekday.rb', line 52

def offset(span, amount, pointer)
  direction = pointer == :future ? 1 : -1

  num_weekdays_passed = 0; offset = 0
  until num_weekdays_passed == amount
    offset += direction * DAY_SECONDS
    num_weekdays_passed += 1 if is_weekday?((span.begin+offset).wday)
  end

  span + offset
end

#this(pointer = :future) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/chronic/repeaters/repeater_weekday.rb', line 41

def this(pointer = :future)
  super

  case pointer
  when :past
    self.next(:past)
  when :future, :none
    self.next(:future)
  end
end

#to_sObject



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

def to_s
  super << '-weekday'
end

#widthObject



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

def width
  DAY_SECONDS
end