avdi / naught

A toolkit for building Null Object classes in Ruby
MIT License
1.05k stars 53 forks source link

Support for NullProxy #64

Open alexpeachey opened 10 years ago

alexpeachey commented 10 years ago

As requested in Issue #6.

I'm completely open to name changes. I just picked some reasonable names to start with.

It doesn't mention it in Issue #6 but we need some way to get the actual result or , otherwise all you have is the proxy. I chose to go with a method called __object__. I'm not thrilled with this name but I also didn't want to pick something that might exist on the wrapped object. Could go with something like __value__ as well.

I also gave it the option to accept a specific type of null object for the default.

Usage:

require 'naught'

NullProxy([]).first.address.country.__object__    # => <null>
NullProxy([1]).first.abs.__object__               # => 1

null = Naught.build do |config|
  config.define_explicit_conversions
end

NullProxy([], null).first.abs.__object__.to_i     # => 0
avdi commented 10 years ago

Thanks Alex! I'll review this when I can.