diff options
Diffstat (limited to 'spec/factory.rb')
-rw-r--r-- | spec/factory.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/spec/factory.rb b/spec/factory.rb new file mode 100644 index 00000000000..29e552b1474 --- /dev/null +++ b/spec/factory.rb @@ -0,0 +1,29 @@ +class Factory + @factories = {} + + class << self + def add(name, klass, &block) + @factories[name] = [klass, block] + end + + def create(name, opts = {}) + new(name, opts).tap(&:save!) + end + + def new(name, opts) + factory = @factories[name] + factory[0].new.tap do |obj| + factory[1].call(obj) + end.tap do |obj| + opts.each do |k, opt| + obj.send("#{k}=", opt) + end + end + end + end +end + +def Factory(name, opts={}) + Factory.create name, opts +end + |