Class: Chronic::RepeaterWeekday

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

Overview

:nodoc:

Constant Summary collapse

WEEK_WEEKDAYS =
5
DAY_SECONDS =

(24 * 60 * 60)

86400

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.



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

def initialize(type)
  super
  @current_weekday_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
# File 'lib/chronic/repeaters/repeater_weekday.rb', line 10

def next(pointer)
  super
  
  direction = pointer == :future ? 1 : -1
  
  if !@current_weekday_start
    @current_weekday_start = Time.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

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

#offset(span, amount, pointer) ⇒ Object



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

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



32
33
34
35
36
37
38
39
40
41
# File 'lib/chronic/repeaters/repeater_weekday.rb', line 32

def this(pointer = :future)
  super
  
  case pointer
  when :past
    self.next(:past)
  when :future, :none
    self.next(:future)
  end
end

#to_sObject



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

def to_s
  super << '-weekday'
end

#widthObject



55
56
57
# File 'lib/chronic/repeaters/repeater_weekday.rb', line 55

def width
  DAY_SECONDS
end