2019-12-18 09:49:57 -06:00
|
|
|
# typed: true
|
2014-02-10 11:45:25 +01:00
|
|
|
module RecurringTodos
|
|
|
|
|
|
|
|
class FormHelper
|
|
|
|
|
|
|
|
def initialize(recurring_todo)
|
|
|
|
@recurring_todo = recurring_todo
|
2014-02-10 22:30:06 +01:00
|
|
|
|
|
|
|
@method_map = {
|
|
|
|
# delegate daily_xxx to daily_pattern.xxx
|
|
|
|
"daily" => {prefix: "", method: daily_pattern},
|
|
|
|
"weekly" => {prefix: "", method: weekly_pattern},
|
|
|
|
"monthly" => {prefix: "", method: monthly_pattern},
|
|
|
|
"yearly" => {prefix: "", method: yearly_pattern},
|
|
|
|
# delegate on_xxx to weekly_pattern.on_xxx
|
|
|
|
"on" => {prefix: "on_", method: weekly_pattern}
|
|
|
|
}
|
2014-02-10 11:45:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_pattern(pattern_class)
|
|
|
|
pattern = pattern_class.new(@recurring_todo.user)
|
|
|
|
pattern.build_from_recurring_todo(@recurring_todo)
|
|
|
|
pattern
|
|
|
|
end
|
|
|
|
|
|
|
|
def daily_pattern
|
2015-02-10 16:25:27 +01:00
|
|
|
@daily_pattern ||= create_pattern(DailyRecurrencePattern)
|
2014-02-10 11:45:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def weekly_pattern
|
2015-02-10 16:25:27 +01:00
|
|
|
@weekly_pattern ||= create_pattern(WeeklyRecurrencePattern)
|
2014-02-10 11:45:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def monthly_pattern
|
2015-02-10 16:25:27 +01:00
|
|
|
@monthly_pattern ||= create_pattern(MonthlyRecurrencePattern)
|
2014-02-10 11:45:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def yearly_pattern
|
2015-02-10 16:25:27 +01:00
|
|
|
@yearly_pattern ||= create_pattern(YearlyRecurrencePattern)
|
2014-02-10 11:45:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *args)
|
2014-02-10 22:30:06 +01:00
|
|
|
# delegate daily_xxx to daily_pattern, weekly_xxx to weekly_pattern, etc.
|
|
|
|
if method.to_s =~ /^([^_]+)_(.+)$/
|
|
|
|
return @method_map[$1][:method].send(@method_map[$1][:prefix]+$2, *args) unless @method_map[$1].nil?
|
2014-02-10 11:45:25 +01:00
|
|
|
end
|
2014-02-10 22:30:06 +01:00
|
|
|
|
|
|
|
# no match, let @recurring_todo handle it, or fail
|
|
|
|
@recurring_todo.send(method, *args)
|
2014-02-10 11:45:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|