{"action":"created","comment":{"url":"https://api.github.com/repos/me-stevens/conman/pulls/comments/51943489","id":51943489,"diff_hunk":"@@ -0,0 +1,32 @@\n+require 'json'\n+\n+class Writer\n+\n+  def initialize(path, file = nil)\n+    @path = path\n+    @file = file\n+  end\n+\n+  def run(contacts)\n+    file.nil? ? write(contacts) : tests_write(contacts)\n+  end\n+\n+  private\n+\n+  attr_reader :path, :file\n+\n+  def parse(contacts)\n+    contacts.to_json\n+  end\n+\n+  def write(contacts)\n+    File.open(path, \"w+\") do |file|","path":"lib/writer.rb","position":23,"original_position":23,"commit_id":"e5e0b9ee768673c3da1d6c38fbfc142cca5c8773","original_commit_id":"e5e0b9ee768673c3da1d6c38fbfc142cca5c8773","user":{"login":"jsuchy","id":6295,"avatar_url":"https://avatars.githubusercontent.com/u/6295?v=3","gravatar_id":"","url":"https://api.github.com/users/jsuchy","html_url":"https://github.com/jsuchy","followers_url":"https://api.github.com/users/jsuchy/followers","following_url":"https://api.github.com/users/jsuchy/following{/other_user}","gists_url":"https://api.github.com/users/jsuchy/gists{/gist_id}","starred_url":"https://api.github.com/users/jsuchy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jsuchy/subscriptions","organizations_url":"https://api.github.com/users/jsuchy/orgs","repos_url":"https://api.github.com/users/jsuchy/repos","events_url":"https://api.github.com/users/jsuchy/events{/privacy}","received_events_url":"https://api.github.com/users/jsuchy/received_events","type":"User","site_admin":false},"body":"If you inject file, you should find it easier to test. Also, this block variable shadows the file attr_reader which makes this class more difficult to reason about.\r\n\r\nExplore the rubydocs for the classes you're using (File / IO in this case), there are other ways to force the file to be written to disk: http://ruby-doc.org/core-2.2.0/IO.html#method-i-flush or http://ruby-doc.org/core-2.2.0/IO.html#method-i-close.","created_at":"2016-02-04T21:50:47Z","updated_at":"2016-02-04T21:50:47Z","html_url":"https://github.com/me-stevens/conman/pull/4#discussion_r51943489","pull_request_url":"https://api.github.com/repos/me-stevens/conman/pulls/4","_links":{"self":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/comments/51943489"},"html":{"href":"https://github.com/me-stevens/conman/pull/4#discussion_r51943489"},"pull_request":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/me-stevens/conman/pulls/4","id":58211169,"html_url":"https://github.com/me-stevens/conman/pull/4","diff_url":"https://github.com/me-stevens/conman/pull/4.diff","patch_url":"https://github.com/me-stevens/conman/pull/4.patch","issue_url":"https://api.github.com/repos/me-stevens/conman/issues/4","number":4,"state":"closed","locked":false,"title":"Reading from and writing to a file","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":"This PR completes the previous one (#3), which made last week's stories incomplete because I hadn't written the contacts to a file and instead I was saving them in an array.\r\n\r\nI still don't know how to test that I'm reading/writing to a file.\r\n\r\nI first introduced the `reader` and `writer` classes in two steps:\r\n* first update the db in a way that doesn't break the action classes (it was using a simple array)\r\n* then use a mock to test the actions (`lister`, `creator`, `finder`) and remove the array from the db\r\n\r\nThe tests were green but when I ran the application it was not reading nor writing to a file.\r\n@sl4m discovered that I would have to:\r\n* Close the file **each time we call the write method**, in order for it to actually write to the file \r\n* Open the file **each time before we call the write method**, so that it wipes the contents, otherwise it appends them and messes the JSON (I was creating the file outside instead, and injecting it as a dependency)\r\n\r\nSo now I need to have something like:\r\n```ruby\r\n  def write(contacts)\r\n    file = File.open(path, \"w+\")\r\n    file.write(parse(contacts))\r\n    file.close\r\n  end\r\n```\r\nbut then it is not testable. My tests didn't pass. In order to make them pass I did a very ugly trick:\r\n```ruby\r\n  def initialize(path, file = nil)\r\n    @path = path\r\n    @file = file\r\n  end\r\n\r\n  def write_contacts(contacts)\r\n    file.nil? ? write(contacts) : tests_write(contacts)\r\n  end\r\n\r\n  def tests_write(contacts)\r\n    file.write(parse(contacts))\r\n  end\r\n```\r\nAfter this change, when I run the application, I don't pass any file in the constructor, and if I run from the tests, I pass a file mock and I test that the `write` method was called. Same for the `reader`.\r\n\r\nI would appreciate any HALP!/advice/hint, since I've been struggling with this for two days now.\r\n\r\n### To do:\r\n\r\n- There is **STILL NO INPUT VALIDATION**.\r\n- Still can't edit contacts, only list them, add them and search.\r\n- Learn how to make better decisions. All my decisions went wrong.\r\n\r\n@jsuchy @christophgockel","created_at":"2016-02-03T22:26:03Z","updated_at":"2016-02-04T21:50:47Z","closed_at":"2016-02-04T13:43:02Z","merged_at":"2016-02-04T13:43:02Z","merge_commit_sha":"bd09032724e635e01b18099ca60122cc1e4cac2f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/me-stevens/conman/pulls/4/commits","review_comments_url":"https://api.github.com/repos/me-stevens/conman/pulls/4/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/4/comments","statuses_url":"https://api.github.com/repos/me-stevens/conman/statuses/e5e0b9ee768673c3da1d6c38fbfc142cca5c8773","head":{"label":"me-stevens:fileio","ref":"fileio","sha":"e5e0b9ee768673c3da1d6c38fbfc142cca5c8773","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-04T20:40:56Z","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":86,"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":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"me-stevens:master","ref":"master","sha":"69655ddaa080493ac4fd237a7e58885b5d7e037a","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-04T20:40:56Z","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":86,"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":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/4"},"html":{"href":"https://github.com/me-stevens/conman/pull/4"},"issue":{"href":"https://api.github.com/repos/me-stevens/conman/issues/4"},"comments":{"href":"https://api.github.com/repos/me-stevens/conman/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/me-stevens/conman/pulls/4/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/4/commits"},"statuses":{"href":"https://api.github.com/repos/me-stevens/conman/statuses/e5e0b9ee768673c3da1d6c38fbfc142cca5c8773"}}}}