Skip to content

Instantly share code, notes, and snippets.

@hamakn
Created February 11, 2014 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamakn/8942175 to your computer and use it in GitHub Desktop.
Save hamakn/8942175 to your computer and use it in GitHub Desktop.
DBがあるときはAR::Baseを継承したクラスのインスタンス、ないときは適当なStructのインスタンスが欲しい
require "activerecord"
require "ostruct"
class Hoge < ActiveRecord::Base
end
class Factory
attr_accessor :klass
def initialize
@klass = OpenStruct
end
def generate(hash)
klass.new hash
end
end
f = Factory.new
a = f.generate({ name: "Foo" })
f.klass = Hoge
b = f.generate({ name: "Bar" })
b.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment