Skip to content

Commit

Permalink
Add NullObject
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Jan 8, 2012
1 parent d9e0372 commit 1c7eab1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/factory_girl.rb
Expand Up @@ -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'
Expand Down
14 changes: 1 addition & 13 deletions lib/factory_girl/attribute_assigner.rb
Expand Up @@ -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)
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions 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

0 comments on commit 1c7eab1

Please sign in to comment.