Skip to content

Instantly share code, notes, and snippets.

@vapidbabble
Forked from stephencelis/minifacture.rb
Created January 12, 2010 19:54
Show Gist options
  • Save vapidbabble/275541 to your computer and use it in GitHub Desktop.
Save vapidbabble/275541 to your computer and use it in GitHub Desktop.
# Factory girl, relaxed.
#
# Factory.define :user do |f|
# f.login 'johndoe%d' # Sequence.
# f.email '%{login}@example.com' # Interpolate.
# f.password f.password_confirmation('foobar') # Chain.
# end
#
# Factory.define :post do |f|
# f.user { Factory :user } # Blocks, if you must.
# end
module Miniskirt
@@factories = {} and mattr_reader :factories
class << self
def define(name)
factories[name.to_s] = {} and yield BasicObject.new.instance_eval(%{
def method_missing(name, value = nil, &block)
::Miniskirt.factories["#{name}"][name] = block || value
end
self
})
end
def build(name, attrs = {})
name = name.to_s and (mod = name.classify.constantize).new do |record|
attrs.stringify_keys!.reverse_update(factories[name]).each do |k, v|
record.send "#{k}=", case v when String # Sequence and interpolate.
v.sub!(/%\d*d/) { |n| n % @n ||= mod.maximum(:id).to_i + 1 }
v % attrs
when Proc then v.call(record) else v
end
end
end
end
def create(name, attrs = {})
build(name, attrs).tap { |record| record.save }
end
end
end
def Miniskirt(name, attrs = {})
Miniskirt.create(name, attrs)
end
Factory = Miniskirt
alias Factory Miniskirt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment