Merge branch 'gem'
[fuzed.git] / helloworld / vendor / gems / chronic-0.2.2 / lib / chronic / repeaters / repeater_day_portion.rb
blobfcb612c6106bf48bb78cb9ed75d40948b16d8b91
1 class Chronic::RepeaterDayPortion < Chronic::Repeater #:nodoc:
2   @@morning = (6 * 60 * 60)..(12 * 60 * 60) # 6am-12am
3   @@afternoon = (13 * 60 * 60)..(17 * 60 * 60) # 1pm-5pm
4   @@evening = (17 * 60 * 60)..(20 * 60 * 60) # 5pm-8pm
5   @@night = (20 * 60 * 60)..(24 * 60 * 60) # 8pm-12pm
6   
7   def initialize(type)
8     super
9     
10     if type.kind_of? Integer
11       @range = (@type * 60 * 60)..((@type + 12) * 60 * 60)
12     else
13       lookup = {:am => 1..(12 * 60 * 60),
14                 :pm => (12 * 60 * 60)..(24 * 60 * 60),
15                 :morning => @@morning,
16                 :afternoon => @@afternoon,
17                 :evening => @@evening,
18                 :night => @@night}
19       @range = lookup[type]
20       lookup[type] || raise("Invalid type '#{type}' for RepeaterDayPortion")
21     end
22     @range || raise("Range should have been set by now")
23   end
24   
25   def next(pointer)
26     super
27     
28     full_day = 60 * 60 * 24
29     
30     if !@current_span
31       now_seconds = @now - Time.construct(@now.year, @now.month, @now.day)
32       if now_seconds < @range.begin
33         case pointer
34         when :future
35           range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
36         when :past
37           range_start = Time.construct(@now.year, @now.month, @now.day) - full_day + @range.begin
38         end
39       elsif now_seconds > @range.end
40         case pointer
41         when :future
42           range_start = Time.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
43         when :past
44           range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
45         end
46       else
47         case pointer
48         when :future
49           range_start = Time.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
50         when :past
51           range_start = Time.construct(@now.year, @now.month, @now.day) - full_day + @range.begin
52         end
53       end
54       
55       @current_span = Chronic::Span.new(range_start, range_start + (@range.end - @range.begin))
56     else
57       case pointer
58       when :future
59         @current_span += full_day
60       when :past
61         @current_span -= full_day
62       end
63     end
64   end
65   
66   def this(context = :future)
67     super
68     
69     range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
70     @current_span = Chronic::Span.new(range_start, range_start + (@range.end - @range.begin))
71   end
72   
73   def offset(span, amount, pointer)
74     @now = span.begin
75     portion_span = self.next(pointer)
76     direction = pointer == :future ? 1 : -1
77     portion_span + (direction * (amount - 1) * Chronic::RepeaterDay::DAY_SECONDS)
78   end
79   
80   def width
81     @range || raise("Range has not been set")
82     return @current_span.width if @current_span
83     if @type.kind_of? Integer
84       return (12 * 60 * 60)
85     else
86       @range.end - @range.begin
87     end
88   end
89   
90   def to_s
91     super << '-dayportion-' << @type.to_s
92   end
93 end