Created
January 27, 2014 20:52
-
-
Save h-akanuma/8657078 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
class Validator | |
include ActiveModel::Validations | |
def self.verify(params, rules) | |
validator = Validator.new params, rules | |
validator.valid? | |
end | |
def initialize(params, rules) | |
rules.each do |key, val| | |
instance_eval <<-DEFINE_VALIDATORS | |
class << self | |
attr_accessor :#{key} | |
validates :#{key}, #{val} | |
end | |
DEFINE_VALIDATORS | |
end | |
params.each do |key, val| | |
instance_eval <<-DEFINE_SETTERS | |
class << self | |
attr_accessor :#{key} | |
end | |
DEFINE_SETTERS | |
end | |
@attributes = params | |
self.attributes = params | |
end | |
def attributes=(attributes) | |
attributes.each do |name, value| | |
send "#{name}=", value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment