Created
September 15, 2010 23:08
-
-
Save skiz/581664 to your computer and use it in GitHub Desktop.
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 'rubygems' | |
require 'active_support' | |
require 'active_model' | |
class Stub | |
def self.generate(name, *attrs) | |
Object::const_set(name.intern, Class::new do | |
include ActiveModel::Serialization | |
include ActiveModel::Validations | |
attr_accessor *attrs | |
validates_presence_of *attrs | |
end | |
) | |
Object::const_get(name).class_eval do | |
def initialize(*attrs) | |
self.send(:attributes=, attrs.extract_options!) | |
end | |
def attributes=(attrs) | |
attrs.each{|k,v| self.send("#{k}=", v) } | |
end | |
end | |
end | |
end | |
Stub.generate('Login', :username, :password) | |
l = Login.new(:username => 'bob') | |
l.valid? | |
puts l.errors.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment