Created
July 20, 2012 10:34
-
-
Save mbj/3150074 to your computer and use it in GitHub Desktop.
Use aequitas as external validator
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
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