Skip to content

Instantly share code, notes, and snippets.

@skiz
Created September 15, 2010 23:08
Show Gist options
  • Save skiz/581664 to your computer and use it in GitHub Desktop.
Save skiz/581664 to your computer and use it in GitHub Desktop.
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