{"action":"created","comment":{"url":"https://api.github.com/repos/me-stevens/conman/pulls/comments/52126441","id":52126441,"diff_hunk":"@@ -9,26 +9,63 @@ def size\n     all.size\n   end\n \n+  def all\n+    reader.nil? ? [] : reader.run\n+  end\n+\n+  def at(index)\n+    empty || invalid(index) ? {} : all[index]\n+  end\n+\n   def add(contact)\n-    contacts = all\n-    contacts << contact\n+    contacts = all << format(contact)\n     writer.run(contacts) if !writer.nil?\n   end\n \n-  def all\n-    reader.nil? ? [] : reader.run\n+  def update(contact)\n+    contacts = updated_with(format(contact))\n+    writer.run(contacts) if !writer.nil?\n   end\n \n-  def at(id)\n-    all.empty? ? {} : all[id]\n+  def search(term)\n+    all.select { |contact| contact.any? {|key, val| val.to_s.include?(term)} }\n   end\n \n-  def search(term)\n-    all.select{ |contact| contact.any? {|key, val| val.include?(term)} }\n+  def index_of_id(id)\n+    all.index { |contact| contact[\"id\"] == id }\n   end\n \n   private\n \n   attr_reader :reader, :writer\n \n+  def empty\n+    all.empty?\n+  end\n+\n+  def invalid(index)\n+    index.nil? ? true : all[index].nil?\n+  end\n+\n+  def format(contact)\n+    contact.has_key?(\"id\") ? contact : append_id(contact, id_of_new)\n+  end\n+\n+  def append_id(contact, id)\n+    contact_with_id       = {}\n+    contact_with_id[\"id\"] = id\n+    contact_with_id.merge!(contact)\n+  end\n+\n+  def id_of_new\n+    size == 0 ? 1 : at(size - 1)[\"id\"] + 1\n+  end\n+\n+  def updated_with(contact)\n+    index    = index_of_id(contact[\"id\"])\n+    contacts = all\n+    invalid(index) ? contacts << contact : contacts[index] = contact","path":"lib/db/db.rb","position":67,"original_position":67,"commit_id":"bcb4ba71d48718baba0bb3489f08e198a5c79155","original_commit_id":"bcb4ba71d48718baba0bb3489f08e198a5c79155","user":{"login":"christophgockel","id":190137,"avatar_url":"https://avatars.githubusercontent.com/u/190137?v=3","gravatar_id":"","url":"https://api.github.com/users/christophgockel","html_url":"https://github.com/christophgockel","followers_url":"https://api.github.com/users/christophgockel/followers","following_url":"https://api.github.com/users/christophgockel/following{/other_user}","gists_url":"https://api.github.com/users/christophgockel/gists{/gist_id}","starred_url":"https://api.github.com/users/christophgockel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/christophgockel/subscriptions","organizations_url":"https://api.github.com/users/christophgockel/orgs","repos_url":"https://api.github.com/users/christophgockel/repos","events_url":"https://api.github.com/users/christophgockel/events{/privacy}","received_events_url":"https://api.github.com/users/christophgockel/received_events","type":"User","site_admin":false},"body":"What's with the ternary operator obsession?\r\n\r\nThe two conditional mutations here are confusion and hard read.\r\nPlease make this more readable and easier for others to parse.","created_at":"2016-02-07T22:41:51Z","updated_at":"2016-02-07T22:41:51Z","html_url":"https://github.com/me-stevens/conman/pull/6#discussion_r52126441","pull_request_url":"https://api.github.com/repos/me-stevens/conman/pulls/6","_links":{"self":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/comments/52126441"},"html":{"href":"https://github.com/me-stevens/conman/pull/6#discussion_r52126441"},"pull_request":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/6"}}},"pull_request":{"url":"https://api.github.com/repos/me-stevens/conman/pulls/6","id":58532630,"html_url":"https://github.com/me-stevens/conman/pull/6","diff_url":"https://github.com/me-stevens/conman/pull/6.diff","patch_url":"https://github.com/me-stevens/conman/pull/6.patch","issue_url":"https://api.github.com/repos/me-stevens/conman/issues/6","number":6,"state":"open","locked":false,"title":"Updater","user":{"login":"me-stevens","id":2157089,"avatar_url":"https://avatars.githubusercontent.com/u/2157089?v=3","gravatar_id":"","url":"https://api.github.com/users/me-stevens","html_url":"https://github.com/me-stevens","followers_url":"https://api.github.com/users/me-stevens/followers","following_url":"https://api.github.com/users/me-stevens/following{/other_user}","gists_url":"https://api.github.com/users/me-stevens/gists{/gist_id}","starred_url":"https://api.github.com/users/me-stevens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/me-stevens/subscriptions","organizations_url":"https://api.github.com/users/me-stevens/orgs","repos_url":"https://api.github.com/users/me-stevens/repos","events_url":"https://api.github.com/users/me-stevens/events{/privacy}","received_events_url":"https://api.github.com/users/me-stevens/received_events","type":"User","site_admin":false},"body":"The addition of the `Updater` took me longer than I expected. I thought a Friday would be enough to finish it, but it ended up taking two days. This is because I realized that, to make my life simpler for the updater, I would had to add unique identifiers to the contacts in the database.\r\n\r\nI was thinking of the case in which somebody deletes the contact while another user is updating it, i.e., I can not rely on the position of the contact in the array to put the edited contact back again, or I would overwrite contacts.\r\n\r\nThis is because I do a fresh read of the database just before adding or updating a new contact, then add the new contact at the end of that array or update that contact, then write the file again.\r\n\r\nThe DB class is in charge of handling these ids, since I think the id is closely related to the db from all the classes I have now.\r\n\r\nRegarding the `add` and `update` methods: \r\n* if the contact is sent to `add`, it should come from `Creator` so it doesn't have an id.\r\n* if the contact is sent to `update`, it should from `Updater`, so it has an id and hopefully an *existing* id.\r\n\r\nTo be *really* safe though, I think the `add` method should probably do the same as the `update` method is doing. But for now I think it's ok.\r\n\r\nAnother thing I don't like is the `nil` in the `app` file. I think I should create an exit screen whose `run` method exits the program. The structure of the app is shown in PR #5.\r\n\r\n# To do:\r\n* Improve the tests for the `Reader` and `Writer` classes.\r\n* Add input validation\r\n* Get rid of the `nil` in the menu\r\n* Felipe thinks my screens should be renamed back to actions","created_at":"2016-02-06T22:45:18Z","updated_at":"2016-02-07T22:41:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6f38765d623ae3c73bf00fde5b514df2c2f55877","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/me-stevens/conman/pulls/6/commits","review_comments_url":"https://api.github.com/repos/me-stevens/conman/pulls/6/comments","review_comment_url":"https://api.github.com/repos/me-stevens/conman/pulls/comments{/number}","comments_url":"https://api.github.com/repos/me-stevens/conman/issues/6/comments","statuses_url":"https://api.github.com/repos/me-stevens/conman/statuses/bcb4ba71d48718baba0bb3489f08e198a5c79155","head":{"label":"me-stevens:updater","ref":"updater","sha":"bcb4ba71d48718baba0bb3489f08e198a5c79155","user":{"login":"me-stevens","id":2157089,"avatar_url":"https://avatars.githubusercontent.com/u/2157089?v=3","gravatar_id":"","url":"https://api.github.com/users/me-stevens","html_url":"https://github.com/me-stevens","followers_url":"https://api.github.com/users/me-stevens/followers","following_url":"https://api.github.com/users/me-stevens/following{/other_user}","gists_url":"https://api.github.com/users/me-stevens/gists{/gist_id}","starred_url":"https://api.github.com/users/me-stevens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/me-stevens/subscriptions","organizations_url":"https://api.github.com/users/me-stevens/orgs","repos_url":"https://api.github.com/users/me-stevens/repos","events_url":"https://api.github.com/users/me-stevens/events{/privacy}","received_events_url":"https://api.github.com/users/me-stevens/received_events","type":"User","site_admin":false},"repo":{"id":50360505,"name":"conman","full_name":"me-stevens/conman","owner":{"login":"me-stevens","id":2157089,"avatar_url":"https://avatars.githubusercontent.com/u/2157089?v=3","gravatar_id":"","url":"https://api.github.com/users/me-stevens","html_url":"https://github.com/me-stevens","followers_url":"https://api.github.com/users/me-stevens/followers","following_url":"https://api.github.com/users/me-stevens/following{/other_user}","gists_url":"https://api.github.com/users/me-stevens/gists{/gist_id}","starred_url":"https://api.github.com/users/me-stevens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/me-stevens/subscriptions","organizations_url":"https://api.github.com/users/me-stevens/orgs","repos_url":"https://api.github.com/users/me-stevens/repos","events_url":"https://api.github.com/users/me-stevens/events{/privacy}","received_events_url":"https://api.github.com/users/me-stevens/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/me-stevens/conman","description":"Contact management app in Ruby","fork":false,"url":"https://api.github.com/repos/me-stevens/conman","forks_url":"https://api.github.com/repos/me-stevens/conman/forks","keys_url":"https://api.github.com/repos/me-stevens/conman/keys{/key_id}","collaborators_url":"https://api.github.com/repos/me-stevens/conman/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/me-stevens/conman/teams","hooks_url":"https://api.github.com/repos/me-stevens/conman/hooks","issue_events_url":"https://api.github.com/repos/me-stevens/conman/issues/events{/number}","events_url":"https://api.github.com/repos/me-stevens/conman/events","assignees_url":"https://api.github.com/repos/me-stevens/conman/assignees{/user}","branches_url":"https://api.github.com/repos/me-stevens/conman/branches{/branch}","tags_url":"https://api.github.com/repos/me-stevens/conman/tags","blobs_url":"https://api.github.com/repos/me-stevens/conman/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/me-stevens/conman/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/me-stevens/conman/git/refs{/sha}","trees_url":"https://api.github.com/repos/me-stevens/conman/git/trees{/sha}","statuses_url":"https://api.github.com/repos/me-stevens/conman/statuses/{sha}","languages_url":"https://api.github.com/repos/me-stevens/conman/languages","stargazers_url":"https://api.github.com/repos/me-stevens/conman/stargazers","contributors_url":"https://api.github.com/repos/me-stevens/conman/contributors","subscribers_url":"https://api.github.com/repos/me-stevens/conman/subscribers","subscription_url":"https://api.github.com/repos/me-stevens/conman/subscription","commits_url":"https://api.github.com/repos/me-stevens/conman/commits{/sha}","git_commits_url":"https://api.github.com/repos/me-stevens/conman/git/commits{/sha}","comments_url":"https://api.github.com/repos/me-stevens/conman/comments{/number}","issue_comment_url":"https://api.github.com/repos/me-stevens/conman/issues/comments{/number}","contents_url":"https://api.github.com/repos/me-stevens/conman/contents/{+path}","compare_url":"https://api.github.com/repos/me-stevens/conman/compare/{base}...{head}","merges_url":"https://api.github.com/repos/me-stevens/conman/merges","archive_url":"https://api.github.com/repos/me-stevens/conman/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/me-stevens/conman/downloads","issues_url":"https://api.github.com/repos/me-stevens/conman/issues{/number}","pulls_url":"https://api.github.com/repos/me-stevens/conman/pulls{/number}","milestones_url":"https://api.github.com/repos/me-stevens/conman/milestones{/number}","notifications_url":"https://api.github.com/repos/me-stevens/conman/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/me-stevens/conman/labels{/name}","releases_url":"https://api.github.com/repos/me-stevens/conman/releases{/id}","deployments_url":"https://api.github.com/repos/me-stevens/conman/deployments","created_at":"2016-01-25T15:36:45Z","updated_at":"2016-01-29T18:51:28Z","pushed_at":"2016-02-07T01:40:36Z","git_url":"git://github.com/me-stevens/conman.git","ssh_url":"git@github.com:me-stevens/conman.git","clone_url":"https://github.com/me-stevens/conman.git","svn_url":"https://github.com/me-stevens/conman","homepage":null,"size":76,"stargazers_count":1,"watchers_count":1,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"master"}},"base":{"label":"me-stevens:master","ref":"master","sha":"0b3986ddb54e77479fe771443f40242bed84be84","user":{"login":"me-stevens","id":2157089,"avatar_url":"https://avatars.githubusercontent.com/u/2157089?v=3","gravatar_id":"","url":"https://api.github.com/users/me-stevens","html_url":"https://github.com/me-stevens","followers_url":"https://api.github.com/users/me-stevens/followers","following_url":"https://api.github.com/users/me-stevens/following{/other_user}","gists_url":"https://api.github.com/users/me-stevens/gists{/gist_id}","starred_url":"https://api.github.com/users/me-stevens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/me-stevens/subscriptions","organizations_url":"https://api.github.com/users/me-stevens/orgs","repos_url":"https://api.github.com/users/me-stevens/repos","events_url":"https://api.github.com/users/me-stevens/events{/privacy}","received_events_url":"https://api.github.com/users/me-stevens/received_events","type":"User","site_admin":false},"repo":{"id":50360505,"name":"conman","full_name":"me-stevens/conman","owner":{"login":"me-stevens","id":2157089,"avatar_url":"https://avatars.githubusercontent.com/u/2157089?v=3","gravatar_id":"","url":"https://api.github.com/users/me-stevens","html_url":"https://github.com/me-stevens","followers_url":"https://api.github.com/users/me-stevens/followers","following_url":"https://api.github.com/users/me-stevens/following{/other_user}","gists_url":"https://api.github.com/users/me-stevens/gists{/gist_id}","starred_url":"https://api.github.com/users/me-stevens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/me-stevens/subscriptions","organizations_url":"https://api.github.com/users/me-stevens/orgs","repos_url":"https://api.github.com/users/me-stevens/repos","events_url":"https://api.github.com/users/me-stevens/events{/privacy}","received_events_url":"https://api.github.com/users/me-stevens/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/me-stevens/conman","description":"Contact management app in Ruby","fork":false,"url":"https://api.github.com/repos/me-stevens/conman","forks_url":"https://api.github.com/repos/me-stevens/conman/forks","keys_url":"https://api.github.com/repos/me-stevens/conman/keys{/key_id}","collaborators_url":"https://api.github.com/repos/me-stevens/conman/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/me-stevens/conman/teams","hooks_url":"https://api.github.com/repos/me-stevens/conman/hooks","issue_events_url":"https://api.github.com/repos/me-stevens/conman/issues/events{/number}","events_url":"https://api.github.com/repos/me-stevens/conman/events","assignees_url":"https://api.github.com/repos/me-stevens/conman/assignees{/user}","branches_url":"https://api.github.com/repos/me-stevens/conman/branches{/branch}","tags_url":"https://api.github.com/repos/me-stevens/conman/tags","blobs_url":"https://api.github.com/repos/me-stevens/conman/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/me-stevens/conman/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/me-stevens/conman/git/refs{/sha}","trees_url":"https://api.github.com/repos/me-stevens/conman/git/trees{/sha}","statuses_url":"https://api.github.com/repos/me-stevens/conman/statuses/{sha}","languages_url":"https://api.github.com/repos/me-stevens/conman/languages","stargazers_url":"https://api.github.com/repos/me-stevens/conman/stargazers","contributors_url":"https://api.github.com/repos/me-stevens/conman/contributors","subscribers_url":"https://api.github.com/repos/me-stevens/conman/subscribers","subscription_url":"https://api.github.com/repos/me-stevens/conman/subscription","commits_url":"https://api.github.com/repos/me-stevens/conman/commits{/sha}","git_commits_url":"https://api.github.com/repos/me-stevens/conman/git/commits{/sha}","comments_url":"https://api.github.com/repos/me-stevens/conman/comments{/number}","issue_comment_url":"https://api.github.com/repos/me-stevens/conman/issues/comments{/number}","contents_url":"https://api.github.com/repos/me-stevens/conman/contents/{+path}","compare_url":"https://api.github.com/repos/me-stevens/conman/compare/{base}...{head}","merges_url":"https://api.github.com/repos/me-stevens/conman/merges","archive_url":"https://api.github.com/repos/me-stevens/conman/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/me-stevens/conman/downloads","issues_url":"https://api.github.com/repos/me-stevens/conman/issues{/number}","pulls_url":"https://api.github.com/repos/me-stevens/conman/pulls{/number}","milestones_url":"https://api.github.com/repos/me-stevens/conman/milestones{/number}","notifications_url":"https://api.github.com/repos/me-stevens/conman/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/me-stevens/conman/labels{/name}","releases_url":"https://api.github.com/repos/me-stevens/conman/releases{/id}","deployments_url":"https://api.github.com/repos/me-stevens/conman/deployments","created_at":"2016-01-25T15:36:45Z","updated_at":"2016-01-29T18:51:28Z","pushed_at":"2016-02-07T01:40:36Z","git_url":"git://github.com/me-stevens/conman.git","ssh_url":"git@github.com:me-stevens/conman.git","clone_url":"https://github.com/me-stevens/conman.git","svn_url":"https://github.com/me-stevens/conman","homepage":null,"size":76,"stargazers_count":1,"watchers_count":1,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/6"},"html":{"href":"https://github.com/me-stevens/conman/pull/6"},"issue":{"href":"https://api.github.com/repos/me-stevens/conman/issues/6"},"comments":{"href":"https://api.github.com/repos/me-stevens/conman/issues/6/comments"},"review_comments":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/6/comments"},"review_comment":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/6/commits"},"statuses":{"href":"https://api.github.com/repos/me-stevens/conman/statuses/bcb4ba71d48718baba0bb3489f08e198a5c79155"}}}}