SafeFinder lets you define a model's
Null Object through a simple DSL, and returns that when you don't find an instance of that model.
Add this line to your application's
And then execute:
Or install it yourself as:
$ gem install safe_finder
Let's say you have a
Post class, and it has
First, you need to include
SafeFinder in your model:
class Post < ActiveRecord::Base include SafeFinder end
Now you can query like this, and if it doesn't find anything it returns a
# It returns a Null Object null_object = Post.safely.find_by_title("Not Exists") null_object.class # NullPost null_object.title # nil null_object.content # nil
Custom Attributes and Methods
And you can customize the Null Object's attributes or methods using a DSL:
class Post < ActiveRecord::Base include SafeFinder safe_attribute :title, "Null" safe_method :some_method do "Do Something" end end
null_object = Post.safely.find_by_title("Not Exists") null_object.title # "Null" null_object.some_method # "Do Something"
Get a Null Object directly
Just simply use:
Post.null_object # <NullPost:0x007fa8a4713be0>
Null Objects inherit
SafeFinder::NullObject, so you can add it in
by generate it with
rails g safe_finder:model
and define general methods for every
module SafeFinder class NullObject def hello "Hello" end end end
null_object = Post.null_object null_object.hello # "Hello"
- Add association support, like
user.postshould also returns
Null Objectwhen it's nil
- More use cases in readme
After checking out the repo, run
bin/setup to install dependencies. Then, run
rake spec to run the tests. You can also run
bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run
bundle exec rake install. To release a new version, update the version number in
version.rb, and then run
bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the
.gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/st0012/SafeFinder.