gitlab-org--gitlab-foss/spec/factory.rb

30 lines
518 B
Ruby
Raw Normal View History

2011-10-08 17:36:38 -04:00
class Factory
@factories = {}
class << self
2011-10-08 17:36:38 -04: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]
2011-10-08 17:36:38 -04:00
factory[0].new.tap do |obj|
factory[1].call(obj)
2011-10-08 17:36:38 -04:00
end.tap do |obj|
opts.each do |k, opt|
obj.send("#{k}=", opt)
2011-10-08 17:36:38 -04:00
end
end
end
end
end
def Factory(name, opts={})
Factory.create name, opts
end