Add NullObject

This commit is contained in:
Joshua Clayton 2012-01-08 00:23:25 -05:00
parent d9e0372345
commit 1c7eab13d6
4 changed files with 29 additions and 13 deletions

View File

@ -3,6 +3,7 @@ require "active_support/core_ext/module/delegation"
require 'factory_girl/proxy'
require 'factory_girl/registry'
require 'factory_girl/null_factory'
require 'factory_girl/null_object'
require 'factory_girl/factory'
require 'factory_girl/attribute_assigner'
require 'factory_girl/evaluator'

View File

@ -20,7 +20,7 @@ module FactoryGirl
end
def hash
@evaluator.instance = null_object.new
@evaluator.instance = NullObject.new
attributes_to_set_on_hash.inject({}) do |result, attribute|
result[attribute] = get(attribute)
@ -34,18 +34,6 @@ module FactoryGirl
@build_class_instance ||= @build_class.new
end
def null_object
Class.new do
instance_methods.each do |m|
undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/
end
def method_missing(*args)
nil
end
end
end
def get(attribute_name)
@evaluator.send(attribute_name)
end

View File

@ -0,0 +1,19 @@
module FactoryGirl
if defined?(::BasicObject)
class NullObject < ::BasicObject
def method_missing(*args)
nil
end
end
else
class NullObject
instance_methods.each do |m|
undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/
end
def method_missing(*args)
nil
end
end
end
end

View File

@ -0,0 +1,8 @@
require "spec_helper"
describe FactoryGirl::NullObject do
its(:id) { should be_nil }
its(:age) { should be_nil }
its(:name) { should be_nil }
its(:admin?) { should be_nil }
end