Class: RecurringTodos::FormHelper

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/recurring_todos/form_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(recurring_todo) ⇒ FormHelper

Returns a new instance of FormHelper.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/recurring_todos/form_helper.rb', line 3

def initialize(recurring_todo)
  @recurring_todo = recurring_todo

  @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 }
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'app/controllers/recurring_todos/form_helper.rb', line 39

def method_missing(method, *args)
  # 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?
  end

  # no match, let @recurring_todo handle it, or fail
  @recurring_todo.send(method, *args)
end

Instance Method Details

#create_pattern(pattern_class) ⇒ Object



17
18
19
20
21
# File 'app/controllers/recurring_todos/form_helper.rb', line 17

def create_pattern(pattern_class)
  pattern = pattern_class.new(@recurring_todo.user)
  pattern.build_from_recurring_todo(@recurring_todo)
  pattern
end

#daily_patternObject



23
24
25
# File 'app/controllers/recurring_todos/form_helper.rb', line 23

def daily_pattern
  @daily_pattern ||= create_pattern(DailyRecurrencePattern)
end

#monthly_patternObject



31
32
33
# File 'app/controllers/recurring_todos/form_helper.rb', line 31

def monthly_pattern
  @monthly_pattern ||= create_pattern(MonthlyRecurrencePattern)
end

#weekly_patternObject



27
28
29
# File 'app/controllers/recurring_todos/form_helper.rb', line 27

def weekly_pattern
  @weekly_pattern ||= create_pattern(WeeklyRecurrencePattern)
end

#yearly_patternObject



35
36
37
# File 'app/controllers/recurring_todos/form_helper.rb', line 35

def yearly_pattern
  @yearly_pattern ||= create_pattern(YearlyRecurrencePattern)
end