File: repeater_day_name.rb

package info (click to toggle)
ruby-chronic 0.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 384 kB
  • sloc: ruby: 3,726; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,222 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Chronic
  class RepeaterDayName < Repeater #:nodoc:
    DAY_SECONDS = 86400 # (24 * 60 * 60)

    def initialize(type)
      super
    end

    def next(pointer)
      super

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

      if !@current_date
        @current_date = Date.new(@now.year, @now.month, @now.day)
        @current_date += direction

        day_num = symbol_to_number(@type)

        while @current_date.wday != day_num
          @current_date += direction
        end
      else
        @current_date += direction * 7
      end
      next_date = @current_date.succ
      Span.new(Chronic.construct(@current_date.year, @current_date.month, @current_date.day), Chronic.construct(next_date.year, next_date.month, next_date.day))
    end

    def this(pointer = :future)
      super

      pointer = :future if pointer == :none
      self.next(pointer)
    end

    def width
      DAY_SECONDS
    end

    def to_s
      super << '-dayname-' << @type.to_s
    end

    private

    def symbol_to_number(sym)
      lookup = {:sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3, :thursday => 4, :friday => 5, :saturday => 6}
      lookup[sym] || raise("Invalid symbol specified")
    end
  end
end