public
Description: Blank is GiraffeSoft’s blank rails starter app.
Homepage:
Clone URL: git://github.com/giraffesoft/blank.git
blank / lib / test_data_factory.rb
100644 29 lines (23 sloc) 0.67 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require 'metaid'
 
module TestDataFactory
  def data_factory(type, data)
    klass = type.to_s.classify.constantize
 
    define_method(:"hash_for_#{type}") do |*args|
      data.merge(args.extract_options!)
    end
 
    meta_def(:"hash_for_#{type}") do |*args|
      data.merge(args.extract_options!)
    end
 
    define_method(:"create_#{type}") do |*args|
      send("build_#{type}", args.extract_options).save!
    end
 
    define_method(:"build_#{type}") do |*args|
      params = send(:"hash_for_#{type}", args.extract_options!)
      returning(klass.new) do |obj|
        params.each do |k, v|
          obj.send("#{k}=", v)
        end
      end
    end
  end
end