lib/rspec_ext/factory.rb in ruby_ext-4.0.2 vs lib/rspec_ext/factory.rb in ruby_ext-4.0.3
- old
+ new
@@ -43,22 +43,43 @@
raise "definition of :#{name} already exist!" if registry.include? name
registry[name] = Builder.new(name, options, &initializer)
end
def build name, attributes = {}, &block
- builder = registry[name] || raise("no definition for :#{name}!")
- builder.build attributes, &block
+ old = creation_method
+ begin
+ self.creation_method ||= :build
+
+ builder = registry[name] || raise("no definition for :#{name}!")
+ builder.build attributes, &block
+ ensure
+ self.creation_method = old
+ end
end
- alias_method :[], :build
def create name, attributes = {}, &block
- o = build name, attributes, &block
- o.respond_to?(:save!) ? o.save! : o.save
- o
+ old = creation_method
+ begin
+ self.creation_method ||= :create
+
+ o = build name, attributes, &block
+ o.respond_to?(:save!) ? o.save! : o.save
+ o
+ ensure
+ self.creation_method = old
+ end
end
+ def auto *args, &block
+ method = creation_method || raise("creation method not defined!")
+ send method, *args, &block
+ end
+
def next name = :general
v = counters[name]
counters[name] += 1
v
end
+
+ protected
+ attr_accessor :creation_method
end
\ No newline at end of file