Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for NullProxy #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,24 @@ MyConsumer.new(null).consume
# => <null>
```

#### Can I avoid hitting nil in a method chain?

Sure thing! Just use a proxy to protect your calls.
You can even optionally specify what kind of null object to use.

```ruby
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
```

#### Are you done yet?

Just one more thing. For maximum convenience, Naught-generated null
Expand Down
1 change: 1 addition & 0 deletions lib/naught.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'naught/version'
require 'naught/null_class_builder'
require 'naught/null_class_builder/commands'
require 'naught/null_proxy'

module Naught
def self.build(&customization_block)
Expand Down
27 changes: 27 additions & 0 deletions lib/naught/null_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Naught
class NullProxy < Naught::BasicObject
def initialize(object, null_class = ::Naught.build)
@object = object
@null_class = null_class
end

def __object__
return @null_class.new if @object.nil?
@object
end

def method_missing(method, *args, &block)
return self if @object.nil?
NullProxy.new(@object.public_send(method, *args, &block), @null_class)
end

def respond_to?(method, include_private = false)
return true if method.to_sym == :__object__
@object.respond_to?(method, include_private)
end
end
end

def NullProxy(object, null_class = ::Naught.build)
Naught::NullProxy.new(object, null_class)
end
68 changes: 68 additions & 0 deletions spec/null_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'spec_helper'

describe Naught::NullProxy do
subject(:proxy) { Naught::NullProxy.new(object) }

describe '__object__' do
context 'with object as an empty array' do
let(:object) { [] }

context 'and calling .first.odd?' do
it 'is a null object' do
expect(proxy.first.odd?.__object__.inspect).to eq '<null>'
end
end

context 'and calling .foo' do
it 'raises a no method error' do
expect { proxy.foo }.to raise_error(NoMethodError)
end
end
end

context 'with object as an array containing the element 1' do
let(:object) { [1] }

context 'and calling .first.odd?' do
it 'is the actual value' do
expect(proxy.first.odd?.__object__).to be true
end
end
end

context 'with a specific null class' do
subject(:proxy) { Naught::NullProxy.new(object, null) }
let(:null) { Naught.build { |config| config.define_explicit_conversions } }

context 'and object as an empty array' do
let(:object) { [] }

context 'and calling .first.odd?' do
it 'is an instance of the specific null class' do
expect(proxy.first.odd?.__object__.class).to eq null
end
end
end
end
end

describe '#respond_to?' do
context 'with object as an empty array' do
let(:object) { [] }

specify { expect(proxy).to respond_to(:size) }

specify { expect(proxy).to respond_to(:__object__) }

specify { expect(proxy).not_to respond_to(:foo) }
end
end
end

describe 'NullProxy()' do
subject(:proxy) { NullProxy([]) }

it 'returns an instance of NullProxy' do
expect(proxy).to be_instance_of(Naught::NullProxy)
end
end