Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

No commit message

  • Loading branch information...
commit 8778b5edb7a2f9befd6053784ec2d3025f6e138e 1 parent 235eb6c
Stephen Celis stephencelis authored

Showing 1 changed file with 23 additions and 6 deletions. Show diff stats Hide diff stats

  1. +23 6 miniskirt.rb
29 miniskirt.rb
... ... @@ -1,19 +1,36 @@
1 1 # Factory girl, relaxed.
  2 +#
  3 +# Factory.define :user do |f|
  4 +# f.login 'johndoe%d' # Sequence.
  5 +# f.email '%{login}@example.com' # Interpolate.
  6 +# f.password f.password_confirmation('foobar') # Chain.
  7 +# end
  8 +#
  9 +# Factory.define :post do |f|
  10 +# f.user { Factory :user } # Blocks, if you must.
  11 +# end
2 12 module Miniskirt
3   - mattr_reader :factories
4   - @@factories = {}
  13 + @@factories = {} and mattr_reader :factories
5 14 class << self
6 15 def define(name)
7 16 factories[name.to_s] = {} and yield BasicObject.new.instance_eval(%{
8   - def method_missing(name, value)
9   - ::Miniskirt.factories["#{name}"][name] = value
  17 + def method_missing(name, value = nil, &block)
  18 + ::Miniskirt.factories["#{name}"][name] = block || value
10 19 end
11 20 self
12 21 })
13 22 end
14 23
15 24 def build(name, attrs = {})
16   - name.to_s.classify.constantize.new factories[name.to_s].merge(attrs)
  25 + name = name.to_s and (mod = name.classify.constantize).new do |record|
  26 + attrs.stringify_keys!.reverse_update(factories[name]).each do |k, v|
  27 + record.send "#{k}=", case v when String # Sequence and interpolate.
  28 + v.sub!(/%\d*d/) { |n| n % @n ||= mod.maximum(:id).to_i + 1 }
  29 + v % attrs
  30 + when Proc then v.call(record) else v
  31 + end
  32 + end
  33 + end
17 34 end
18 35
19 36 def create(name, attrs = {})
@@ -27,4 +44,4 @@ def Miniskirt(name, attrs = {})
27 44 end
28 45
29 46 Factory = Miniskirt
30   -alias Factory Miniskirt
  47 +alias Factory Miniskirt

0 comments on commit 8778b5e

Please sign in to comment.
Something went wrong with that request. Please try again.