Class: TodoPresenter
- Inherits:
-
Object
- Object
- TodoPresenter
- 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
-
#active_todo_count ⇒ Object
Returns the value of attribute active_todo_count.
-
#can_clear_completed ⇒ Object
Returns the value of attribute can_clear_completed.
-
#created_todo ⇒ Object
Returns the value of attribute created_todo.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#new_todo ⇒ Object
readonly
Returns the value of attribute new_todo.
-
#todos ⇒ Object
readonly
Returns the value of attribute todos.
Instance Method Summary collapse
- #active_todos ⇒ Object
- #apply_route_filter ⇒ Object
- #clear_completed ⇒ Object
- #completed_todos ⇒ Object
- #create_todo ⇒ Object
- #destroy(todo) ⇒ Object
-
#initialize ⇒ TodoPresenter
constructor
A new instance of TodoPresenter.
- #setup_filter_routes ⇒ Object
- #toggle_all_completed ⇒ Object
- #unsetup_filter_routes ⇒ Object
Constructor Details
#initialize ⇒ TodoPresenter
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_count ⇒ Object
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_completed ⇒ Object
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_todo ⇒ Object
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 |
#filter ⇒ Object
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_todo ⇒ Object (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 |
#todos ⇒ Object (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_todos ⇒ Object
21 |
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 21 def active_todos = todos.select(&:active?) |
#apply_route_filter ⇒ Object
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_completed ⇒ Object
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_todos ⇒ Object
23 |
# File 'lib/glimmer-dsl-web/samples/regular/todo_mvc/presenters/todo_presenter.rb', line 23 def completed_todos = todos.select(&:completed?) |
#create_todo ⇒ Object
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_routes ⇒ Object
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_completed ⇒ Object
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_routes ⇒ Object
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 |