Class: Chronic::RepeaterMonthName

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

Overview

:nodoc:

Constant Summary collapse

MONTH_SECONDS =

30 * 24 * 60 * 60

2_592_000

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

Returns a new instance of RepeaterMonthName.



4
5
6
7
# File 'lib/chronic/repeaters/repeater_month_name.rb', line 4

def initialize(type)
  super
  @current_month_begin = nil
end

Instance Method Details

#indexObject



73
74
75
# File 'lib/chronic/repeaters/repeater_month_name.rb', line 73

def index
  symbol_to_number(@type)
end

#next(pointer) ⇒ Object



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
53
54
55
56
# File 'lib/chronic/repeaters/repeater_month_name.rb', line 9

def next(pointer)
  super
  
  if !@current_month_begin
    target_month = symbol_to_number(@type)
    case pointer
    when :future
      if @now.month < target_month
        @current_month_begin = Time.construct(@now.year, target_month)
      elsif @now.month > target_month
        @current_month_begin = Time.construct(@now.year + 1, target_month)
      end
    when :none
      if @now.month <= target_month
        @current_month_begin = Time.construct(@now.year, target_month)
      elsif @now.month > target_month
        @current_month_begin = Time.construct(@now.year + 1, target_month)
      end
    when :past
      if @now.month > target_month
        @current_month_begin = Time.construct(@now.year, target_month)
      elsif @now.month < target_month
        @current_month_begin = Time.construct(@now.year - 1, target_month)
      end
    end
    @current_month_begin || raise("Current month should be set by now")
  else
    case pointer
    when :future
      @current_month_begin = Time.construct(@current_month_begin.year + 1, @current_month_begin.month)
    when :past
      @current_month_begin = Time.construct(@current_month_begin.year - 1, @current_month_begin.month)
    end
  end
  
  cur_month_year = @current_month_begin.year
  cur_month_month = @current_month_begin.month
  
  if cur_month_month == 12
    next_month_year = cur_month_year + 1
    next_month_month = 1
  else
    next_month_year = cur_month_year
    next_month_month = cur_month_month + 1
  end
    
  Chronic::Span.new(@current_month_begin, Time.construct(next_month_year, next_month_month))
end

#this(pointer = :future) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/chronic/repeaters/repeater_month_name.rb', line 58

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

#to_sObject



77
78
79
# File 'lib/chronic/repeaters/repeater_month_name.rb', line 77

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

#widthObject



69
70
71
# File 'lib/chronic/repeaters/repeater_month_name.rb', line 69

def width
  MONTH_SECONDS
end