如何ActiveRecord的外部创建ActiveRecord的风格验证?
我工作的一个测试框架为我公司编写的软件。我们的产品是一个基于Web和之后,我运行一个基于REST的请求,我要处理的结果。我希望能有ActiveRecord的验证类型在每个命令类,以便它运行后的结果会自动对所有的验证测试。但是,我不知道如何做到这一点。我的code看起来像这样(简化以示出的重要组成部分)。
I am working on a testing framework for the software that my company writes. Our product is web based and After I run a RESTful request I want to process the results. I want to be able to have activerecord type validations in each command class so that after it is run the results are automatically tested against all the "validations". However, I am not sure how to do this. My code looks like this (simplified to show the important parts).
class CodesecureCommand
def execute
result = RestClient.post("http://#{codesecure.host_name_port}#{path}", post_data)
return parse(result) #parse simple returns a Hpricot document
end
end
class RunScan < CodesecureCommand
#What I have now
#I have to override the execute function so that it calls the local success method
#to see if it failed or not.
def execute()
result = super()
if success(result)
return true
else
end
end
def success(result)
result.search('div.transaction-message') do |message|
if message.innerHTML.scan(/Configure abuse setting for domain users successfully\./).length == 1
return true
end
end
end
#What I would like is to be able to call execute (without having to override it).
#then after it runs it calls back to this class to check
#if the regex matches the command was successful and returns true
test_success /regex/
#if test_success fails then these are called
#the idea being that I can use the regex to identify errors that happened then
#report them to the user
identify_error /regex/, "message"
identify_error /regex/, "message"
end
end
我要的是,执行方法被调用后test_success和identify_error被自动调用像的ActiveRecord的验证。谁能告诉我该怎么做呢?谢谢
What I want is that after the execute method is called the test_success and identify_error are automatically called like the validations in activerecord. Can anybody tell me how to do this? Thanks
而不必看了很多你的code,这是我作为在实施验证类方法:
Without having looked much at your code, here's my take on implementing validation class methods:
module Validations
def self.included(base)
base.extend ClassMethods
end
def validate
errors.clear
self.class.validations.each {|validation| validation.call(self) }
end
def valid?
validate
errors.blank?
end
def errors
@errors ||= {}
end
module ClassMethods
def validations
@validations ||= []
end
def validates_presence_of(*attributes)
validates_attributes(*attributes) do |instance, attribute, value, options|
instance.errors[attribute] = "cant't be blank" if value.blank?
end
end
def validates_format_of(*attributes)
validates_attributes(*attributes) do |instance, attribute, value, options|
instance.errors[attribute] = "is invalid" unless value =~ options[:with]
end
end
def validates_attributes(*attributes, &proc)
options = attributes.extract_options!
validations << Proc.new { |instance|
attributes.each {|attribute|
proc.call(instance, attribute, instance.__send__(attribute), options)
}
}
end
end
end
它假定的ActiveSupport周围,它是在Rails环境。你可能会想扩展它允许每个属性的多个错误,用 instance.errors [属性]&LT;&LT; 消息
,但我离开了这样的晦涩,为了保持这一简短的样本越简单越好。
It assumes that ActiveSupport is around, which it is in a Rails environment. You might want to extend it to allow multiple errors per attribute, with instance.errors[attribute] << "the message"
, but I left out obscurities like that in order to keep this short sample as simple as possible.
下面是一个简短的使用例子:
Here's a short usage example:
class MyClass
include Validations
attr_accessor :foo
validates_presence_of :foo
validates_format_of :foo, :with => /^[a-z]+$/
end
a = MyClass.new
puts a.valid?
# => false
a.foo = "letters"
puts a.valid?
# => true
a.foo = "Oh crap$(!)*#"
puts a.valid?
# => false