From 5f3b1e267a464bc85832a0eb05189f6c7a7a51cf Mon Sep 17 00:00:00 2001 From: Michael Kompanets Date: Thu, 2 Apr 2015 08:57:41 -0700 Subject: [PATCH 1/3] added errors class folder --- lib/to_nil/errors/could_not_nil.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 lib/to_nil/errors/could_not_nil.rb diff --git a/lib/to_nil/errors/could_not_nil.rb b/lib/to_nil/errors/could_not_nil.rb new file mode 100644 index 0000000..30a2a31 --- /dev/null +++ b/lib/to_nil/errors/could_not_nil.rb @@ -0,0 +1,11 @@ +module ToNil + module Errors + class CouldNotNil < StandardError + attr_reader :object + def initialize(message, object) + super(message) + @object = object + end + end + end +end \ No newline at end of file From 6bbb8f20572405146f594f9ff7c08452680b9bb5 Mon Sep 17 00:00:00 2001 From: Michael Kompanets Date: Thu, 2 Apr 2015 08:58:08 -0700 Subject: [PATCH 2/3] added errors require and to_nil with a bang method --- lib/to_nil.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/to_nil.rb b/lib/to_nil.rb index 1d0a22e..5a4f45c 100644 --- a/lib/to_nil.rb +++ b/lib/to_nil.rb @@ -1,10 +1,18 @@ require "to_nil/version" +# Errors +require "to_nil/errors/could_not_nil" + module ToNil module ObjectExtension def to_nil nil end + + def to_nil! + self.to_nil == nil || raise(Errors::CouldNotNil.new("refused to nil", self)) + nil + end end end From c13fb73764d1e3bdaa83ef4daea55ab34ca03f17 Mon Sep 17 00:00:00 2001 From: Michael Kompanets Date: Thu, 2 Apr 2015 08:58:45 -0700 Subject: [PATCH 3/3] to nil with a bang spec --- spec/to_nil_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/to_nil_spec.rb b/spec/to_nil_spec.rb index 6ddbf92..e7e6e63 100644 --- a/spec/to_nil_spec.rb +++ b/spec/to_nil_spec.rb @@ -9,9 +9,22 @@ it "returns nil for a #{klass} instance" do expect(klass.new.to_nil).to be_nil end + + it "returns nil for a #{klass} instance with a bang" do + expect(klass.new.to_nil!).to be_nil + end + + it "raises an error when #{klass} instance is not nil with a bang" do + allow_any_instance_of(klass).to receive(:to_nil).and_return('Not Nil!') + expect{klass.new.to_nil!}.to raise_error + end end it "returns nil for nil.to_nil" do expect(nil.to_nil).to be_nil end + + it "returns nil for nil.to_nil with a bang" do + expect(nil.to_nil!).to be_nil + end end