GithubHelp home page GithubHelp logo

aishek / nihilist Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 3.0 26 KB

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

Home Page: https://rubygems.org/gems/nigilist

Ruby 100.00%
ruby ruby-on-rails null-object-pattern

nihilist's Introduction

Nigilist

Gem Version Build Status Code Climate

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

gem install nigilist

Usage

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:

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

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 😉

nihilist's People

Contributors

aishek avatar inem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

inem mhoncharov

nihilist's Issues

Provide a way to mimic AR associations

If you have associations in your original model, then in order to make your code work well with Null object you need to fo something like this:

class NullCompany
  include Nigilist

  def devices
    Device.none
  end
end

I am not sure if it makes sense to add this functionality, but at least it worth mentioning in Readme

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.