This project is archived and is in readonly mode.
:allow_nil option for delegate
Reported by porras | September 27th, 2008 @ 08:43 PM | in 2.x
Adds :allow_nil option to delegate method:
# If the object in which you delegate can be nil, you may want to use the
# :allow_nil option. In that case, it returns nil instead of raising a
# NoMethodError exception:
#
# class Foo
# attr_accessor :bar
# def initialize(bar = nil)
# @bar = bar
# end
# delegate :zoo, :to => :bar
# end
#
# Foo.new.zoo # raises NoMethodError exception (you called nil.zoo)
#
# class Foo
# attr_accessor :bar
# def initialize(bar = nil)
# @bar = bar
# end
# delegate :zoo, :to => :bar, :allow_nil => true
# end
#
# Foo.new.zoo # returns nil
#
Comments and changes to this ticket
-
Daniel Schierbeck September 29th, 2008 @ 02:23 PM
+1: This seems like a good addition to
delegate
, and I like the simplicity of the implementation. -
David Röthlisberger October 14th, 2008 @ 02:06 PM
- Useful, and the patch is very simple.
If anything, I'd change the rdoc to say "If the object to which you delegate...".
-
David Röthlisberger October 14th, 2008 @ 02:07 PM
(Um, that "1." in the comment above was supposed to be a "+1").
-
Josh Susser October 14th, 2008 @ 03:30 PM
-1
I dislike silently hiding errors like that. Seems like just asking for trouble.
-
David Röthlisberger October 14th, 2008 @ 04:46 PM
How is it hiding the error any more than:
def contact_name; contact && contact.name; end
It's intended as a shortcut for the case where not having a contact isn't an error. Remember you have to specify :allow_nil, this isn't going to be default behaviour.
-
porras December 8th, 2008 @ 12:09 AM
I updated the patch, which no longer applied after the :prefix option was added.
@Josh, as Dave explained, this option is intended only for the cases where this is not an error but a legitimate situation, just to avoid that verbose and ugly definition.
-
Daniel Schierbeck December 16th, 2008 @ 07:02 PM
I'd like some more attention brought on this ticket (i.e. BUMP), as I've recently found use cases for it abound.
I currently have the following simplified setup:
# A resident may or may not have a current residentship -- he # could be a former resident. class Resident < ActiveRecord::Base has_many :residentships delegate :room_number, :room, :to => :residentship def residentship residentships.only_current.first end end class Residentship < ActiveRecord::Base belongs_to :resident validates_presence_of :room_number, :moved_out_at named_scope :only_current, ... composed_of :room, :mapping => %w(room_number number) end class Room # value class, as there's a finite set of rooms def residentship Residentship.only_current.find_by_room_number(@number) end delegate :resident, :to => :residentships end
I'd like
Room#resident
,Resident#room
andResident#room_number
(as well as several more in my real app) to work properly for former residents, i.e. just return nil -- but with the current implementation ofdelegate
, they raise an exception instead. Adding an:allow_nil
option todelegate
would greatly reduce the number of extra methods i need to have. -
Repository December 22nd, 2008 @ 12:14 AM
- State changed from new to resolved
(from [e8de7a67a5ef063164da022845a7cae1753da80e]) Add :allow_nil option to delegate [#1127 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
-
yoyogo March 31st, 2011 @ 05:59 AM
oh my!! i love you so much!!
Thank you so much.
Hermes Bags
http://www.louisvuittonparty.com/
Hermes Bags -
slina March 31st, 2011 @ 06:36 AM
iPad 2 Video Converter is perfectly compatible with iPad 2
iPad 2 Video Converter Mac is your best choice to convert videos to iPad 2 on Mac OS X.
DVD to iPad 2 is specially designed for Apple users.
DVD to iPad 2 Mac is a wonderful gift for Mac and iPad users
Play 1080P Blu-ray movies with Blu-ray to iPad 2 Converter
Blu-ray to iPad 2 Mac is definitely your best choice to convert Blu-ray DVD to iPad 2 for playback with with its large and high-resolution screen.
Blu-ray to Nintendo 3DS Converter can easily rip/convert Blu-ray DVD, even protected DVDs and encrypted AACS/ BD+ Blu-ray movies to Nintendo 3DS easily and quickly.
DVD to Nintendo 3DS, actally an all-in-one Blu-ray DVD Converter, enables users to convert Blu-ray (.m2ts) ) or DVD movies (.vob) or ISO image files or IFO to Nintendo 3DS/NDSL/ DS, iPhone 4, iPad 2, Motorola Xoom, Android Phones, iPod Touch, Xbox 360, PSP, PS3, Motorola Droid, Nexus One, HTC EVO 4G, etc for entertainment freely.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 984 Make it possible to alias delegation methods daniel: you're completely right. I've already done that (...
- 1080 More options to make delegate more flexible This is now a duplicate of bugs #1127 and #984.
- 1127 :allow_nil option for delegate (from [e8de7a67a5ef063164da022845a7cae1753da80e]) Add :al...