Class: RecurringTodos::YearlyRepeatPattern

Inherits:
AbstractRepeatPattern show all
Defined in:
app/models/recurring_todos/yearly_repeat_pattern.rb

Instance Attribute Summary

Attributes inherited from AbstractRepeatPattern

#attributes

Instance Method Summary collapse

Methods inherited from AbstractRepeatPattern

#build_from_recurring_todo, #build_recurring_todo, #continues_recurring?, #day_of_week_as_text, #determine_start, #end_date, #ends_on, #errors, #find_last_day_x_of_month, #find_xth_day_of_month, #get, #get_due_date, #get_show_from_date, #get_xth_day_of_month, #month_of_year_as_text, #number_of_occurences, #put_in_tickler?, #recurring_target_as_text, #set_recurrence_on_validations, #show_always?, #show_from_delta, #start_from, #starts_and_ends_on_validations, #target, #update_recurring_todo, #valid?, #validate_not_blank, #validate_not_nil, #xth

Constructor Details

#initialize(user) ⇒ YearlyRepeatPattern


5
6
7
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 5

def initialize(user)
  super user
end

Instance Method Details

#date_as_month_dayObject (private)


78
79
80
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 78

def date_as_month_day
  I18n.l(DateTime.new(Time.zone.now.year, month_of_year, every_x_day), :format => :month_day)
end

#day_of_weekObject


25
26
27
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 25

def day_of_week
  get :every_count
end

#every_x_dayObject


17
18
19
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 17

def every_x_day
  get :every_other1
end

#every_xth_dayObject


21
22
23
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 21

def every_xth_day
  get :every_other3
end

#get_next_date(previous) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 63

def get_next_date(previous)
  start = determine_start(previous)
  month = get(:every_other2)

  case recurrence_selector
  when 0 # specific day of a specific month
    return get_specific_day_of_month(start, month)
  when 1 # relative weekday of a specific month
    return get_relative_weekday_of_month(start, month)
  end
  nil
end

#get_relative_weekday_of_month(start, month) ⇒ Object (private)


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 94

def get_relative_weekday_of_month(start, month)
  # if there is no next month n in this year, search in next year
  the_next = start.month > month ? Time.zone.local(start.year+1, month, 1) : start

  # get the xth day of the month
  the_next = get_xth_day_of_month(self.every_xth_day, day_of_week, month, the_next.year)

  # if the_next is before previous, we went back into the past, so try next
  # year
  the_next = get_xth_day_of_month(self.every_xth_day, day_of_week, month, start.year+1) if the_next <= start

  the_next
end

#get_specific_day_of_month(start, month) ⇒ Object (private)


82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 82

def get_specific_day_of_month(start, month)
  if start.month > month || (start.month == month && start.day >= every_x_day)
    # if there is no next month n and day m in this year, search in next
    # year
    start = Time.zone.local(start.year+1, month, 1)
  else
    # if there is a next month n, stay in this year
    start = Time.zone.local(start.year, month, 1)
  end
  Time.zone.local(start.year, month, every_x_day)
end

#month_of_yearObject


13
14
15
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 13

def month_of_year
  get :every_other2
end

#month_of_year2Object


29
30
31
32
33
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 29

def month_of_year2
  # if recurrence pattern is every xth day in a month, return month otherwise
  # return a default value
  get(:recurrence_selector) == 1 ? get(:every_other2) : Time.zone.now.month
end

#recurrence_patternObject


35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 35

def recurrence_pattern
  if self.recurrence_selector == 0
    I18n.t("todos.recurrence.pattern.every_year_on", :date => date_as_month_day)
  else
    I18n.t("todos.recurrence.pattern.every_year_on",
      :date => I18n.t("todos.recurrence.pattern.the_xth_day_of_month",
        :x => xth(every_xth_day),
        :day => day_of_week_as_text(day_of_week),
        :month => month_of_year_as_text(month_of_year)
      ))
  end
end

#recurrence_selectorObject


9
10
11
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 9

def recurrence_selector
  get :recurrence_selector
end

#validateObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/recurring_todos/yearly_repeat_pattern.rb', line 48

def validate
  super
  case recurrence_selector
  when 0 # 'yearly_every_x_day'
    validate_not_blank(month_of_year, "The month of the year may not be empty for recurrence setting")
    validate_not_blank(every_x_day, "The day of the month may not be empty for recurrence setting")
  when 1 # 'yearly_every_xth_day'
    validate_not_blank(month_of_year2, "The month of the year may not be empty for recurrence setting")
    validate_not_blank(every_xth_day, "The nth day of the month may not be empty for recurrence setting")
    validate_not_blank(day_of_week, "The day of the week may not be empty for recurrence setting")
  else
    raise "unexpected value of recurrence selector '#{recurrence_selector}'"
  end
end