Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Nigilist Ruby gem — Null Object for User model in your app

NotificationsYou must be signed in to change notification settings

aishek/nihilist

Repository files navigation

Gem VersionBuild StatusCode Climate

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:

classGuestdefadmin?falseendendcurrent_user=current_session.user ||Guest.newcurrent_user.admin?

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.

Contributors

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 fromlib/nihilist.rb 😉

About

Nigilist Ruby gem — Null Object for User model in your app

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp