Skip to content

Instantly share code, notes, and snippets.

@mbj
Created July 20, 2012 10:34
Show Gist options
  • Save mbj/3150074 to your computer and use it in GitHub Desktop.
Save mbj/3150074 to your computer and use it in GitHub Desktop.
Use aequitas as external validator
module ExternalValidator
def self.included(descendant)
descendant.class_eval do
InstanceMethods
end
end
module InstanceMethods
def initialize(object)
@object = object
end
def method_missing(name,*args,&block)
super if args.length != 0
@object[name]
end
end
end
class User
class Validator
include ExternalValidator
end
include Virtus
attribute :firstname, String
Validator.validates_presence_of :firstname
def validator
Valiator.new(self)
end
def valid?
validator.valid?
end
end
user = User.new(:firstname => "John")
user.valid? # => true
user.firtname = nil
user.valid? # => false
validator = user.validator
validator.errors # Access error information
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment