Skip to content
This repository
Browse code

Add NullObject

  • Loading branch information...
commit 1c7eab13d635806040bcd65cb626552cc54871f7 1 parent d9e0372
Joshua Clayton authored January 08, 2012
1  lib/factory_girl.rb
@@ -3,6 +3,7 @@
3 3
 require 'factory_girl/proxy'
4 4
 require 'factory_girl/registry'
5 5
 require 'factory_girl/null_factory'
  6
+require 'factory_girl/null_object'
6 7
 require 'factory_girl/factory'
7 8
 require 'factory_girl/attribute_assigner'
8 9
 require 'factory_girl/evaluator'
14  lib/factory_girl/attribute_assigner.rb
@@ -20,7 +20,7 @@ def object
20 20
     end
21 21
 
22 22
     def hash
23  
-      @evaluator.instance = null_object.new
  23
+      @evaluator.instance = NullObject.new
24 24
 
25 25
       attributes_to_set_on_hash.inject({}) do |result, attribute|
26 26
         result[attribute] = get(attribute)
@@ -34,18 +34,6 @@ def build_class_instance
34 34
       @build_class_instance ||= @build_class.new
35 35
     end
36 36
 
37  
-    def null_object
38  
-      Class.new do
39  
-        instance_methods.each do |m|
40  
-          undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/
41  
-        end
42  
-
43  
-        def method_missing(*args)
44  
-          nil
45  
-        end
46  
-      end
47  
-    end
48  
-
49 37
     def get(attribute_name)
50 38
       @evaluator.send(attribute_name)
51 39
     end
19  lib/factory_girl/null_object.rb
... ...
@@ -0,0 +1,19 @@
  1
+module FactoryGirl
  2
+  if defined?(::BasicObject)
  3
+    class NullObject < ::BasicObject
  4
+      def method_missing(*args)
  5
+        nil
  6
+      end
  7
+    end
  8
+  else
  9
+    class NullObject
  10
+      instance_methods.each do |m|
  11
+        undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/
  12
+      end
  13
+
  14
+      def method_missing(*args)
  15
+        nil
  16
+      end
  17
+    end
  18
+  end
  19
+end
8  spec/factory_girl/null_object_spec.rb
... ...
@@ -0,0 +1,8 @@
  1
+require "spec_helper"
  2
+
  3
+describe FactoryGirl::NullObject do
  4
+  its(:id)     { should be_nil }
  5
+  its(:age)    { should be_nil }
  6
+  its(:name)   { should be_nil }
  7
+  its(:admin?) { should be_nil }
  8
+end

0 notes on commit 1c7eab1

Please sign in to comment.
Something went wrong with that request. Please try again.