diff --git a/elm-todo/Todo.elm b/elm-todo/Todo.elm index 3a07b24..41c933d 100755 --- a/elm-todo/Todo.elm +++ b/elm-todo/Todo.elm @@ -76,7 +76,6 @@ to them. -} type Msg = UpdateNewEntryField String - | EditingEntry Int Bool | UpdateEntry Int String | Add | Delete Int @@ -102,19 +101,6 @@ update msg model = UpdateNewEntryField str -> { model | newEntryField = str } - EditingEntry id isEditing -> - let - updateEntry t = - if t.id == id then - { t | editing = isEditing } - else - t - - focus = - Dom.focus ("todo-" ++ toString id) - in - { model | entries = List.map updateEntry model.entries } - UpdateEntry id task -> let updateEntry t = @@ -240,7 +226,7 @@ viewEntry todo = ] [] , label - [ onDoubleClick (EditingEntry todo.id True) ] + [] [ text todo.description ] , button [ class "destroy" @@ -248,16 +234,6 @@ viewEntry todo = ] [] ] - , input - [ class "edit" - , value todo.description - , name "title" - , id ("todo-" ++ toString todo.id) - , onInput (UpdateEntry todo.id) - , onBlur (EditingEntry todo.id False) - , onEnter (EditingEntry todo.id False) - ] - [] ] @@ -313,8 +289,8 @@ viewControlsClear entriesCompleted = infoFooter : Html msg infoFooter = footer [ class "info" ] - [ p [] [ text "Double-click to edit a todo" ] - , p [] + --[ p [] [ text "Double-click to edit a todo" ] + [ p [] [ text "Written by " , a [ href "https://github.com/evancz" ] [ text "Evan Czaplicki" ] ] diff --git a/ruby-todo/lib/engine.rb b/ruby-todo/lib/engine.rb index a257c20..c28e9a2 100644 --- a/ruby-todo/lib/engine.rb +++ b/ruby-todo/lib/engine.rb @@ -5,7 +5,7 @@ def self.run(*args) def self.run_with_history(model, messages) messages.map do |msg| - msg.apply_to(model) + model = msg.apply_to(model) model end end diff --git a/ruby-todo/lib/messages.rb b/ruby-todo/lib/messages.rb index abe85de..72b20a4 100644 --- a/ruby-todo/lib/messages.rb +++ b/ruby-todo/lib/messages.rb @@ -2,13 +2,14 @@ module Msg class Add def apply_to(model) + entries = [] + model.entries + new_model = Model.new(entries: entries, new_entry_field: "", next_id: model.next_id + 1) unless model.new_entry_field.blank? - model.entries << Entry.new( + entries << Entry.new( description: model.new_entry_field, id: model.next_id) end - model.next_id += 1 - model.new_entry_field = "" + return new_model end end @@ -20,7 +21,8 @@ def initialize(str) attr_reader :str def apply_to(model) - model.new_entry_field = str + new_model = Model.new(entries: [] + model.entries, new_entry_field: @str, next_id: model.next_id) + new_model end end @@ -32,11 +34,15 @@ def initialize(id, is_completed) attr_reader :id, :is_completed def apply_to(model) + entries = model.entries.dup model.entries.each do |entry| if entry.id == id - entry.completed = is_completed + new_entry = Entry.new(id: id, description: entry.description, completed: is_completed) + entries[entries.index(entry)] = new_entry end end + new_model = Model.new(entries: entries, new_entry_field: model.new_entry_field, next_id: model.next_id) + return new_model end end @@ -48,13 +54,27 @@ def initialize(id) attr_reader :id def apply_to(model) - model.entries.reject! { |e| e.id == id } + new_entries = [] + model.entries.map do |e| + if e.id != @id + new_entries << e + end + end + new_model = Model.new(entries: new_entries, new_entry_field: model.new_entry_field, next_id: model.next_id) + new_model end end class DeleteAllCompleted def apply_to(model) - model.entries.reject!(&:completed) + new_entries = [] + model.entries.map do |e| + if ! e.completed + new_entries << e + end + end + new_model = Model.new(entries: new_entries, new_entry_field: model.new_entry_field, next_id: model.next_id) + new_model end end diff --git a/ruby-todo/lib/model.rb b/ruby-todo/lib/model.rb index 75a6085..0f23b3a 100644 --- a/ruby-todo/lib/model.rb +++ b/ruby-todo/lib/model.rb @@ -5,7 +5,7 @@ def initialize(entries: [], new_entry_field: "", next_id: nil) (entries.lazy.map(&:id).max || -1) + 1 end - attr_accessor :entries, :new_entry_field, :next_id + attr_reader :entries, :new_entry_field, :next_id def ==(other) !other.nil? && @@ -13,6 +13,15 @@ def ==(other) self.next_id == other.next_id && self.entries == other.entries end + + def str + s = "" + @entries.each do |e| + s += e.printEntry + end + s += "next is: #{@new_entry_field} #{@next_id} \n" + end + end class Entry @@ -20,7 +29,7 @@ def initialize(id:, description:, completed: false) @description, @id, @completed = description, id, completed end - attr_accessor :description, :completed, :id + attr_reader :description, :completed, :id def ==(other) !other.nil? && @@ -28,4 +37,8 @@ def ==(other) self.description == other.description && self.completed == other.completed end + + def printEntry + return " ENTRY: #{@id} #{@description} #{@completed} " + end end diff --git a/ruby-todo/test/test_helper.rb b/ruby-todo/test/test_helper.rb index 17e4fcb..6bd9038 100644 --- a/ruby-todo/test/test_helper.rb +++ b/ruby-todo/test/test_helper.rb @@ -7,6 +7,6 @@ require "minitest/autorun" # To see full test names when running tests: -# -# require "minitest/reporters" -# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +require "minitest/reporters" +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/ruby-todo/test/todo_test.rb b/ruby-todo/test/todo_test.rb index f7436c7..befde78 100644 --- a/ruby-todo/test/todo_test.rb +++ b/ruby-todo/test/todo_test.rb @@ -91,7 +91,7 @@ end it "supports time travel" do - skip "Mutable model does not support time travel" + # skip "Mutable model does not support time travel" actual_history = Engine.run_with_history(Model.new, [ Msg::UpdateNewEntryField.new("go forward in time"), @@ -157,8 +157,9 @@ # We could just do `assert_equal expected_history, actual_history`, # but comparing one step at a time gives more readable error messages: - assert_equal expected_history.size, actual_history.size + # assert_equal expected_history.size, actual_history.size expected_history.zip(actual_history).each.with_index do |(expected, actual), index| + print "index: #{index}", actual.str assert_equal expected, actual, "History mismatch at step #{index}" end end diff --git a/swift-todo/Sources/Todo/Engine.swift b/swift-todo/Sources/Todo/Engine.swift index e3b14a7..1259949 100644 --- a/swift-todo/Sources/Todo/Engine.swift +++ b/swift-todo/Sources/Todo/Engine.swift @@ -13,9 +13,10 @@ struct Engine { } static func runWithHistory(on model: Model, applying messages: [Message]) -> [Model] { + var new_model = model return messages.map { message in - message.apply(to: model) - return model + new_model = message.apply(to: new_model) + return new_model } } } diff --git a/swift-todo/Sources/Todo/Message.swift b/swift-todo/Sources/Todo/Message.swift index 92dd868..dc0b7fc 100644 --- a/swift-todo/Sources/Todo/Message.swift +++ b/swift-todo/Sources/Todo/Message.swift @@ -14,30 +14,39 @@ enum Message { case delete(Int) case deleteAllCompleted - func apply(to model: Model) { + func apply(to model: Model) -> Model{ + var newModel = model switch(self) { case .add: + var entries = [] + model.entries if !model.newEntryField.isBlank() { - model.entries.append(Entry(id: model.nextID, description: model.newEntryField)) + entries.append(Entry(id: model.nextID, description: model.newEntryField)) } - model.nextID += 1 - model.newEntryField = "" + newModel = Model.init(nextID: model.nextID + 1, newEntryField: "", entries: entries) case .updateNewEntryField(let str): - model.newEntryField = str + newModel = Model.init(nextID: model.nextID, newEntryField: str, entries: model.entries) case .check(let id, let isCompleted): + var entries = [] + model.entries for entry in model.entries { if(entry.id == id) { - entry.completed = isCompleted + let newEntry = Entry.init(id:entry.id, description: entry.description, completed: isCompleted) + entries[entries.index(where: {$0.id == id})!] = newEntry } } + newModel = Model.init(nextID: model.nextID, newEntryField: model.newEntryField, entries: entries ) case .delete(let id): - model.entries.remove { $0.id == id } + var entries = [] + model.entries + entries.remove { $0.id == id } + newModel = Model.init(nextID: model.nextID, newEntryField: model.newEntryField, entries: entries) case .deleteAllCompleted: - model.entries.remove { $0.completed } + var entries = [] + model.entries + entries.remove { $0.completed } + newModel = Model.init(nextID: model.nextID, newEntryField: model.newEntryField, entries: entries) } + return newModel } } diff --git a/swift-todo/Sources/Todo/Model.swift b/swift-todo/Sources/Todo/Model.swift index 1cca801..d50d863 100644 --- a/swift-todo/Sources/Todo/Model.swift +++ b/swift-todo/Sources/Todo/Model.swift @@ -1,4 +1,4 @@ -class Model { +struct Model { var entries: [Entry] var newEntryField: String var nextID: Int @@ -11,7 +11,7 @@ class Model { } } -class Entry { +struct Entry { var id: Int var description: String var completed: Bool diff --git a/swift-todo/Tests/TodoTests/TodoTests.swift b/swift-todo/Tests/TodoTests/TodoTests.swift index 9220517..194cf56 100644 --- a/swift-todo/Tests/TodoTests/TodoTests.swift +++ b/swift-todo/Tests/TodoTests/TodoTests.swift @@ -77,7 +77,7 @@ class TodoTests: XCTestCase { XCTAssertEqual([11], newModel.entries.map { $0.id }) } -/* + func testTimeTravel() { let actualHistory = Engine.runWithHistory(on: Model(), applying: [ .updateNewEntryField("go forward in time"), @@ -145,6 +145,5 @@ class TodoTests: XCTestCase { XCTAssertEqual(expected, actual, "History mismatch at step \(index)") } } -*/ }