Class: Chronic::RepeaterDayPortion

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

Overview

:nodoc:

Constant Summary collapse

@@morning =

6am-12am

(6 * 60 * 60)..(12 * 60 * 60)
@@afternoon =

1pm-5pm

(13 * 60 * 60)..(17 * 60 * 60)
@@evening =

5pm-8pm

(17 * 60 * 60)..(20 * 60 * 60)
@@night =

8pm-12pm

(20 * 60 * 60)..(24 * 60 * 60)

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

Returns a new instance of RepeaterDayPortion.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chronic/repeaters/repeater_day_portion.rb', line 7

def initialize(type)
  super
  @current_span = nil

  if type.kind_of? Integer
    @range = (@type * 60 * 60)..((@type + 12) * 60 * 60)
  else
    lookup = {:am => 0..(12 * 60 * 60 - 1),
              :pm => (12 * 60 * 60)..(24 * 60 * 60 - 1),
              :morning => @@morning,
              :afternoon => @@afternoon,
              :evening => @@evening,
              :night => @@night}
    @range = lookup[type]
    lookup[type] || raise("Invalid type '#{type}' for RepeaterDayPortion")
  end
  @range || raise("Range should have been set by now")
end

Instance Method Details

#next(pointer) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chronic/repeaters/repeater_day_portion.rb', line 26

def next(pointer)
  super

  full_day = 60 * 60 * 24

  if !@current_span
    now_seconds = @now - Time.construct(@now.year, @now.month, @now.day)
    if now_seconds < @range.begin
      case pointer
      when :future
        range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
      when :past
        range_start = Time.construct(@now.year, @now.month, @now.day) - full_day + @range.begin
      end
    elsif now_seconds > @range.end
      case pointer
      when :future
        range_start = Time.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
      when :past
        range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
      end
    else
      case pointer
      when :future
        range_start = Time.construct(@now.year, @now.month, @now.day) + full_day + @range.begin
      when :past
        range_start = Time.construct(@now.year, @now.month, @now.day) - full_day + @range.begin
      end
    end

    @current_span = Chronic::Span.new(range_start, range_start + (@range.end - @range.begin))
  else
    case pointer
    when :future
      @current_span += full_day
    when :past
      @current_span -= full_day
    end
  end
end

#offset(span, amount, pointer) ⇒ Object



74
75
76
77
78
79
# File 'lib/chronic/repeaters/repeater_day_portion.rb', line 74

def offset(span, amount, pointer)
  @now = span.begin
  portion_span = self.next(pointer)
  direction = pointer == :future ? 1 : -1
  portion_span + (direction * (amount - 1) * Chronic::RepeaterDay::DAY_SECONDS)
end

#this(context = :future) ⇒ Object



67
68
69
70
71
72
# File 'lib/chronic/repeaters/repeater_day_portion.rb', line 67

def this(context = :future)
  super

  range_start = Time.construct(@now.year, @now.month, @now.day) + @range.begin
  @current_span = Chronic::Span.new(range_start, range_start + (@range.end - @range.begin))
end

#to_sObject



91
92
93
# File 'lib/chronic/repeaters/repeater_day_portion.rb', line 91

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

#widthObject



81
82
83
84
85
86
87
88
89
# File 'lib/chronic/repeaters/repeater_day_portion.rb', line 81

def width
  @range || raise("Range has not been set")
  return @current_span.width if @current_span
  if @type.kind_of? Integer
    return (12 * 60 * 60)
  else
    @range.end - @range.begin
  end
end