lib/object_factory.rb in rahoulb-object-factory-0.1.1 vs lib/object_factory.rb in rahoulb-object-factory-0.1.2
- old
+ new
@@ -2,11 +2,11 @@
require 'rujitsu'
class Object
# return an instance of an Object::Factory
def self.factory
- @@object_factory ||= Factory.new
+ THE_OBJECT_FACTORY_INSTANCE
end
# Factory allows test suites to build new instances of objects, specifying some simple constraints on certain fields
# If a new instance is created via the factory then that instance can have specialist values automatically applied to given fields, meaning that it should be possible for test cases to build valid objects without having to specify a full valid field-set
# The factory should not be created directly, but instead accessed through the Object#factory method.
@@ -84,10 +84,20 @@
end
#ÊError raised when create_and_save_a cannot save the object
class CannotSaveError < RuntimeError; end
+ # print the rules for a given class
+ def print_configuration_for klass
+ fields_and_generators = @generators[symbol_for(klass)]
+ unless fields_and_generators.nil?
+ fields_and_generators.each do | field_name, generator |
+ puts "#{field_name} uses a lambda"
+ end
+ end
+ end
+
private
def symbol_for object
klass = object.is_a?(Class) ? object : object.class
return klass.name.to_sym
@@ -147,10 +157,11 @@
value = proc.call
instance.send("#{field_name.to_sym}=".to_sym, value) unless parameters.has_key?(field_name.to_sym)
end
end
end
+
end
# Short-cut method for Object::Factory#create_a
# Also aliased as an, for class names that start with a vowel.
# instance = a Thingy
@@ -170,5 +181,7 @@
def when_creating_a klass, options = {}
Object.factory.when_creating_a klass, options
end
alias when_creating_an when_creating_a
+
+THE_OBJECT_FACTORY_INSTANCE = Object::Factory.new