Created
January 4, 2011 02:59
-
-
Save leeadkins/764326 to your computer and use it in GitHub Desktop.
A lil somethin somethin
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
class Fun | |
def errors | |
@errors ||= [] | |
end | |
def valid? | |
@errors.clear | |
self.class.validations.each do |method, validation| | |
if instance_eval(&validation[:prc]) == false | |
@errors << {:validator => method, :message => validation[:message]} | |
end | |
end | |
return @errors.empty? | |
end | |
class << self | |
def validations | |
@validations ||= Hash.new{|h,k| h[k]={} } | |
end | |
def validates(method, options={}, &block) | |
self.validations[method][:prc] = block | |
self.validations[method][:message] = options[:message] != nil ? options[:message] : "Invalid" | |
end | |
end | |
end | |
#Validators need to | |
class Have < Fun | |
attr_accessor :cat | |
attr_accessor :cat_id | |
attr_accessor :email | |
validates :cat_id_or_email, :message => "Cat_id or Email must be present" do | |
(self.cat_id != nil) || (self.email != nil) | |
end | |
validates :cat_is_nil do | |
self.cat == nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment