Created
February 11, 2014 19:22
-
-
Save hamakn/8942175 to your computer and use it in GitHub Desktop.
DBがあるときはAR::Baseを継承したクラスのインスタンス、ないときは適当なStructのインスタンスが欲しい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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