https://github.com/aishek/nihilist
Nigilist Ruby gem — Null Object for User model in your app
https://github.com/aishek/nihilist
null-object-pattern ruby ruby-on-rails
Last synced: 5 months ago
JSON representation
Nigilist Ruby gem — Null Object for User model in your app
- Host: GitHub
- URL: https://github.com/aishek/nihilist
- Owner: aishek
- Created: 2018-02-05T11:48:04.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-09T22:25:07.000Z (about 3 years ago)
- Last Synced: 2024-10-30T04:36:49.893Z (over 1 year ago)
- Topics: null-object-pattern, ruby, ruby-on-rails
- Language: Ruby
- Homepage: https://rubygems.org/gems/nigilist
- Size: 25.4 KB
- Stars: 11
- Watchers: 3
- Forks: 3
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Nigilist
[](https://rubygems.org/gems/nigilist)
[](https://travis-ci.org/aishek/nihilist)
[](https://codeclimate.com/github/aishek/nihilist)
Nigilist is extremely useful for implement Null Object pattern. It allows null object to return `false` for all question-methods, `nil` for all non-bang-methods, `[]` for all methods in plural form (and in Ruby on Rails null-relation for same name ActiveRecord model) without defining them.
## Installation
```bash
gem install nigilist
```
## Usage
```ruby
class Order < ActiveRecord::Base
end
class Guest
include Nigilist
def polite?
true
end
def locale
'ru'
end
end
current_user = Guest.new
current_user.admin? # false — for all question methods
current_user.polite? # true — becase it is explicit defined
current_user.orders # Order.none — for all plural methods because same singular name ActiveRecord model exists
current_user.planets # [] — for all plural methods without because singular name ActiveRecord model doesn't exists
current_user.address # nil — for all non question, non plural and non bang methods
current_user.locale # 'ru' — becase it is explicit defined
```
### Explanation of Null Object pattern
For example, you have a `User` model, with `#admin?` method, and `Guest` model allows you use same interface for both authorized and unauthorized users like so:
```ruby
class Guest
def admin?
false
end
end
current_user = current_session.user || Guest.new
current_user.admin?
```
So when your project grows and other methods got it place inside `User` model you have to define same methods in `Guest` model which contains no logic but only return `false` or `nil`. In that case Nigilist could be helpful.
## License
Nigilist is free software, and may be redistributed under the MIT License.
## Contributors
* [Alexandr Borisov](https://github.com/aishek)
* [Ivan Nemytchenko](https://github.com/inem)
## The gem about one method
The gem contains exactly one module with one method defined: `method_missing`. I'm sure you able to implement it by yourself in couple of minutes. So feel free just to copy it from `lib/nihilist.rb` 😉