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

Returns a new instance of RepeaterWeekday.



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

def initialize(type)
  super
end

Instance Method Details

#next(pointer) ⇒ Object



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

def next(pointer)
  super

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

  if !@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



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

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



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

def this(pointer = :future)
  super

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

#to_sObject



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

def to_s
  super << '-weekday'
end

#widthObject



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

def width
  DAY_SECONDS
end