Class: Chronic::RepeaterTime

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

Overview

:nodoc:

Defined Under Namespace

Classes: Tick

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_times, scan_for_units

Methods inherited from Tag

#start=

Constructor Details

#initialize(time, options = {}) ⇒ RepeaterTime

Returns a new instance of RepeaterTime.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chronic/repeaters/repeater_time.rb', line 27

def initialize(time, options = {})
  t = time.gsub(/\:/, '')
  @type = 
  if (1..2) === t.size
    Tick.new(t.to_i * 60 * 60, true)
  elsif t.size == 3
    Tick.new((t[0..0].to_i * 60 * 60) + (t[1..2].to_i * 60), true)
  elsif t.size == 4
    ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
    Tick.new(t[0..1].to_i * 60 * 60 + t[2..3].to_i * 60, ambiguous)
  elsif t.size == 5
    Tick.new(t[0..0].to_i * 60 * 60 + t[1..2].to_i * 60 + t[3..4].to_i, true)
  elsif t.size == 6
    ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
    Tick.new(t[0..1].to_i * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous)
  else
    raise("Time cannot exceed six digits")
  end
end

Instance Method Details

#next(pointer) ⇒ Object

Return the next past or future Span for the time that this Repeater represents

pointer - Symbol representing which temporal direction to fetch the next day
          must be either :past or :future


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chronic/repeaters/repeater_time.rb', line 50

def next(pointer)
  super
  
  half_day = 60 * 60 * 12
  full_day = 60 * 60 * 24
  
  first = false
  
  unless @current_time
    first = true
    midnight = Time.local(@now.year, @now.month, @now.day)
    yesterday_midnight = midnight - full_day
    tomorrow_midnight = midnight + full_day

    catch :done do
      if pointer == :future
        if @type.ambiguous?
          [midnight + @type, midnight + half_day + @type, tomorrow_midnight + @type].each do |t|
            (@current_time = t; throw :done) if t > @now
          end
        else
          [midnight + @type, tomorrow_midnight + @type].each do |t|
            (@current_time = t; throw :done) if t > @now
          end
        end
      else # pointer == :past
        if @type.ambiguous?
          [midnight + half_day + @type, midnight + @type, yesterday_midnight + @type * 2].each do |t|
            (@current_time = t; throw :done) if t < @now
          end
        else
          [midnight + @type, yesterday_midnight + @type].each do |t|
            (@current_time = t; throw :done) if t < @now
          end
        end
      end
    end
    
    @current_time || raise("Current time cannot be nil at this point")
  end
  
  unless first
    increment = @type.ambiguous? ? half_day : full_day
    @current_time += pointer == :future ? increment : -increment
  end
  
  Chronic::Span.new(@current_time, @current_time + width)
end

#this(context = :future) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/chronic/repeaters/repeater_time.rb', line 99

def this(context = :future)
  super
  
  context = :future if context == :none
  
  self.next(context)
end

#to_sObject



111
112
113
# File 'lib/chronic/repeaters/repeater_time.rb', line 111

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

#widthObject



107
108
109
# File 'lib/chronic/repeaters/repeater_time.rb', line 107

def width
  1
end