You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Nigilist is extremely useful for implement Null Object pattern. It allows null object to returnfalse 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
gem install nigilist
Usage
classOrder <ActiveRecord::BaseendclassGuestincludeNigilistdefpolite?trueenddeflocale'ru'endendcurrent_user=Guest.newcurrent_user.admin?# false — for all question methodscurrent_user.polite?# true — becase it is explicit definedcurrent_user.orders# Order.none — for all plural methods because same singular name ActiveRecord model existscurrent_user.planets# [] — for all plural methods without because singular name ActiveRecord model doesn't existscurrent_user.address# nil — for all non question, non plural and non bang methodscurrent_user.locale# 'ru' — becase it is explicit defined
Explanation of Null Object pattern
For example, you have aUser model, with#admin? method, andGuest model allows you use same interface for both authorized and unauthorized users like so:
So when your project grows and other methods got it place insideUser model you have to define same methods inGuest model which contains no logic but only returnfalse ornil. In that case Nigilist could be helpful.
License
Nigilist is free software, and may be redistributed under the MIT License.
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 fromlib/nihilist.rb 😉
About
Nigilist Ruby gem — Null Object for User model in your app