Permalink
Browse files

Add NullFactory

  • Loading branch information...
1 parent 41bc3ac commit aee300aa90a82970c1fa73df48bbc8305b99532a @joshuaclayton joshuaclayton committed Oct 29, 2011
Showing with 42 additions and 12 deletions.
  1. +1 −0 lib/factory_girl.rb
  2. +11 −12 lib/factory_girl/factory.rb
  3. +19 −0 lib/factory_girl/null_factory.rb
  4. +11 −0 spec/factory_girl/null_factory_spec.rb
View
@@ -6,6 +6,7 @@
require 'factory_girl/proxy/attributes_for'
require 'factory_girl/proxy/stub'
require 'factory_girl/registry'
+require 'factory_girl/null_factory'
require 'factory_girl/factory'
require 'factory_girl/attribute'
require 'factory_girl/attribute/static'
@@ -29,7 +29,7 @@ def build_class #:nodoc:
end
def default_strategy #:nodoc:
- @default_strategy || (parent && parent.default_strategy) || :create
+ @default_strategy || parent.default_strategy || :create
end
def allow_overrides
@@ -89,17 +89,15 @@ def names
end
def compile
- if parent
- parent.defined_traits.each {|trait| define_trait(trait) }
- parent.compile
- end
+ parent.defined_traits.each {|trait| define_trait(trait) }
+ parent.compile
attribute_list.ensure_compiled
end
protected
def class_name #:nodoc:
- @class_name || (parent && parent.class_name) || name
+ @class_name || parent.class_name || name
end
def attributes
@@ -110,14 +108,12 @@ def attributes
end
list.apply_attribute_list(attribute_list)
- list.apply_attribute_list(parent.attributes) if parent
+ list.apply_attribute_list(parent.attributes)
end
end
def callbacks
- [traits.map(&:callbacks), @definition.callbacks].tap do |result|
- result.unshift(*parent.callbacks) if parent
- end.flatten
+ [parent.callbacks, traits.map(&:callbacks), @definition.callbacks].flatten
end
private
@@ -137,8 +133,11 @@ def traits
end
def parent
- return unless @parent
- FactoryGirl.factory_by_name(@parent)
+ if @parent
+ FactoryGirl.factory_by_name(@parent)
+ else
+ NullFactory.new
+ end
end
def attribute_list
@@ -0,0 +1,19 @@
+module FactoryGirl
+ class NullFactory
+ attr_reader :definition
+
+ def initialize
+ @definition = Definition.new
+ end
+
+ delegate :defined_traits, :callbacks, :to => :definition
+
+ def compile; end
+ def default_strategy; end
+ def class_name; end
+
+ def attributes
+ AttributeList.new
+ end
+ end
+end
@@ -0,0 +1,11 @@
+require "spec_helper"
+
+describe FactoryGirl::NullFactory do
+ it { should delegate(:defined_traits).to(:definition) }
+ it { should delegate(:callbacks).to(:definition) }
+
+ its(:compile) { should be_nil }
+ its(:default_strategy) { should be_nil }
+ its(:class_name) { should be_nil }
+ its(:attributes) { should be_an_instance_of(FactoryGirl::AttributeList) }
+end

0 comments on commit aee300a

Please sign in to comment.