Class: TodoPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb

Constant Summary collapse

FILTERS =
[:all, :active, :completed]
FILTER_ROUTE_REGEXP =
/\#\/([^\/]*)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTodoPresenter

Returns a new instance of TodoPresenter.



12
13
14
15
16
17
18
19
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 12

def initialize
  @todos = []
  @new_todo = Todo.new(task: '')
  @filter = :all
  @can_clear_completed = false
  @active_todo_count = 0
  todo_stat_refresh_observer.observe(todos) # refresh stats if todos array adds/removes todo objects
end

Instance Attribute Details

#active_todo_countObject

Returns the value of attribute active_todo_count.



9
10
11
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 9

def active_todo_count
  @active_todo_count
end

#can_clear_completedObject

Returns the value of attribute can_clear_completed.



9
10
11
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 9

def can_clear_completed
  @can_clear_completed
end

#created_todoObject

Returns the value of attribute created_todo.



9
10
11
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 9

def created_todo
  @created_todo
end

#filterObject

Returns the value of attribute filter.



10
11
12
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 10

def filter
  @filter
end

#new_todoObject (readonly)

Returns the value of attribute new_todo.



10
11
12
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 10

def new_todo
  @new_todo
end

#todosObject (readonly)

Returns the value of attribute todos.



10
11
12
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 10

def todos
  @todos
end

Instance Method Details

#active_todosObject



21
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 21

def active_todos = todos.select(&:active?)

#apply_route_filterObject



62
63
64
65
66
67
68
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 62

def apply_route_filter
  route_filter_match = $$.document.location.href.to_s.match(FILTER_ROUTE_REGEXP)
  return if route_filter_match.nil?
  route_filter = route_filter_match[1]
  route_filter = 'all' if route_filter == ''
  self.filter = route_filter
end

#clear_completedObject



42
43
44
45
46
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 42

def clear_completed
  refresh_todo_stats do
    completed_todos.each { |todo| delete(todo) }
  end
end

#completed_todosObject



23
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 23

def completed_todos = todos.select(&:completed?)

#create_todoObject



25
26
27
28
29
30
31
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 25

def create_todo
  todo = new_todo.clone
  todos.append(todo)
  observe_todo_completion_to_update_todo_stats(todo)
  new_todo.task = ''
  self.created_todo = todo # notifies View observer indirectly to add created todo to todo list
end

#destroy(todo) ⇒ Object



38
39
40
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 38

def destroy(todo)
  delete(todo)
end

#setup_filter_routesObject



56
57
58
59
60
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 56

def setup_filter_routes
  @filter_router_function = -> (event) { apply_route_filter }
  $$.addEventListener('popstate', &@filter_router_function)
  apply_route_filter
end

#toggle_all_completedObject



48
49
50
51
52
53
54
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 48

def toggle_all_completed
  target_completed_value = active_todos.any?
  todos_to_update = target_completed_value ? active_todos : completed_todos
  refresh_todo_stats do
    todos_to_update.each { |todo| todo.completed = target_completed_value }
  end
end

#unsetup_filter_routesObject



70
71
72
73
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 70

def unsetup_filter_routes
  $$.removeEventListener('popstate', &@filter_router_function)
  @filter_router_function = nil
end