Skip to content

ingoweiss/null_object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

NullObject
==========

Adds a :null_object option to ActiveRecord has_one and belongs_to associations for specifying an object to be returned in place of nil

Implements the null object pattern as described in 'Refactoring - Ruby Edition' (Addison-Wesley)

Just a very experimental start - use at your own peril. For feedback please contact me at ingo@ingoweiss.com

Example
=======

user.country = nil
user.country.name
=> 'Missing country'

classs User < ActiveRecord::Base

  belongs_to :country, :null_object => MissingCountry.instance
  
  def missing?
    false
  end
  
end

class MissingCountry
  
  def missing?
    true
  end
  
  def name
    'Unknown country'
  end
  
end


Copyright (c) 2008 Ingo Weiss, released under the MIT license

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages