gitlabhq/spec/factory.rb

30 lines
514 B
Ruby
Raw Normal View History

2011-10-08 23:36:38 +02:00
class Factory
@factories = {}
class << self
2011-10-08 23:36:38 +02:00
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)
2011-10-08 23:36:38 +02:00
end.tap do |obj|
opts.each do |k, opt|
obj.send("#{k}=", opt)
2011-10-08 23:36:38 +02:00
end
end
end
end
end
def Factory(name, opts={})
Factory.create name, opts
end