Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds to_nil! method with a bang #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/to_nil.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
11 changes: 11 additions & 0 deletions lib/to_nil/errors/could_not_nil.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions spec/to_nil_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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