Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add NullObject

  • Loading branch information...
commit 1c7eab13d635806040bcd65cb626552cc54871f7 1 parent d9e0372
@joshuaclayton joshuaclayton authored
View
1  lib/factory_girl.rb
@@ -3,6 +3,7 @@
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
14 lib/factory_girl/attribute_assigner.rb
@@ -20,7 +20,7 @@ def object
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 @@ def build_class_instance
@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
19 lib/factory_girl/null_object.rb
@@ -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
8 spec/factory_girl/null_object_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.