From 80534fbb00c9da496438f73a0cb8a11b5a461a7c Mon Sep 17 00:00:00 2001 From: Wade West Date: Sun, 7 Nov 2010 01:05:46 -0500 Subject: [PATCH] The find method will now return nil when a note doesn't exist. Added the find! method which will raise a CatchNotes::NotFound error. --- lib/catch_notes/base.rb | 8 +++++++- test/test_catch_notes.rb | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/catch_notes/base.rb b/lib/catch_notes/base.rb index e7c42c4..bf495c8 100644 --- a/lib/catch_notes/base.rb +++ b/lib/catch_notes/base.rb @@ -54,12 +54,18 @@ def all end end - def find(id) + def find!(id) res = get "/notes/#{id}" if send(:ok?, res) send :build_from_hash, res.parsed_response['notes'].first end end + + def find(id) + find!(id) + rescue CatchNotes::NotFound + nil + end def first all.first diff --git a/test/test_catch_notes.rb b/test/test_catch_notes.rb index b5aa47a..1300a79 100644 --- a/test/test_catch_notes.rb +++ b/test/test_catch_notes.rb @@ -60,9 +60,14 @@ def setup assert !note.destroy end - should "raise a NotFound error when a note doesn't exist" do + should "raise a nil error when a note doesn't exist: find" do + assert_nil NoteClass.find 13 + end + + + should "raise a NotFound error when a note doesn't exist: find!" do assert_raise CatchNotes::NotFound do - NoteClass.find 13 + NoteClass.find! 13 end end