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


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

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


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

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


19
20
21
22
23
# File 'app/controllers/recurring_todos/form_helper.rb', line 19

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

#daily_patternObject


25
26
27
# File 'app/controllers/recurring_todos/form_helper.rb', line 25

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

#monthly_patternObject


33
34
35
# File 'app/controllers/recurring_todos/form_helper.rb', line 33

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

#weekly_patternObject


29
30
31
# File 'app/controllers/recurring_todos/form_helper.rb', line 29

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

#yearly_patternObject


37
38
39
# File 'app/controllers/recurring_todos/form_helper.rb', line 37

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