Skip to content

Instantly share code, notes, and snippets.

@h-akanuma
Created January 27, 2014 20:52
Show Gist options
  • Save h-akanuma/8657078 to your computer and use it in GitHub Desktop.
Save h-akanuma/8657078 to your computer and use it in GitHub Desktop.
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