Class: Chronic::RepeaterMonth

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

Overview

:nodoc:

Constant Summary collapse

MONTH_SECONDS =

30 * 24 * 60 * 60

2_592_000
YEAR_MONTHS =
12

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

Returns a new instance of RepeaterMonth.



7
8
9
10
# File 'lib/chronic/repeaters/repeater_month.rb', line 7

def initialize(type)
  super
  @current_month_start = nil
end

Instance Method Details

#next(pointer) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/chronic/repeaters/repeater_month.rb', line 12

def next(pointer)
  super
  
  if !@current_month_start
    @current_month_start = offset_by(Time.construct(@now.year, @now.month), 1, pointer)
  else
    @current_month_start = offset_by(Time.construct(@current_month_start.year, @current_month_start.month), 1, pointer)
  end
  
  Chronic::Span.new(@current_month_start, Time.construct(@current_month_start.year, @current_month_start.month + 1))
end

#offset(span, amount, pointer) ⇒ Object



42
43
44
# File 'lib/chronic/repeaters/repeater_month.rb', line 42

def offset(span, amount, pointer)      
  Chronic::Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
end

#offset_by(time, amount, pointer) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chronic/repeaters/repeater_month.rb', line 46

def offset_by(time, amount, pointer) 
  direction = pointer == :future ? 1 : -1
  
  amount_years = direction * amount / YEAR_MONTHS
  amount_months = direction * amount % YEAR_MONTHS
  
  new_year = time.year + amount_years
  new_month = time.month + amount_months
  if new_month > YEAR_MONTHS
    new_year += 1
    new_month -= YEAR_MONTHS
  end
  
  new_time = Time.construct(new_year, new_month, time.day, time.hour, time.min, time.sec)
  if new_time.month > new_month
    d = Date.new(new_year, new_month, -1)
    Time.construct(new_year, d.month, d.day, time.hour, time.min, time.sec)
  else
    new_time
  end
end

#this(pointer = :future) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chronic/repeaters/repeater_month.rb', line 24

def this(pointer = :future)
  super
  
  case pointer
  when :future
    month_start = Time.construct(@now.year, @now.month, @now.day + 1)
    month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
  when :past
    month_start = Time.construct(@now.year, @now.month)
    month_end = Time.construct(@now.year, @now.month, @now.day)
  when :none
    month_start = Time.construct(@now.year, @now.month)
    month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
  end
  
  Chronic::Span.new(month_start, month_end)
end

#to_sObject



72
73
74
# File 'lib/chronic/repeaters/repeater_month.rb', line 72

def to_s
  super << '-month'
end

#widthObject



68
69
70
# File 'lib/chronic/repeaters/repeater_month.rb', line 68

def width
  MONTH_SECONDS
end