2009-04-22 73 views
1

我正在爲我公司編寫的軟件測試框架。我們的產品是基於Web的,在我運行RESTful請求後,我想處理結果。我希望能夠在每個命令類中進行activerecord類型驗證,以便在運行後自動針對所有「驗證」對結果進行測試。但是,我不知道如何做到這一點。我的代碼看起來像這樣(簡化以顯示重要部分)。如何在activerecord之外創建activerecord風格驗證?

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的驗證。任何人都可以告訴我如何做到這一點?由於

回答

3

而不必多看了你的代碼,這是我拿上實施驗證類方法:

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環境。您可能想要擴展它以允許每個屬性出現多個錯誤,但是我忽略了這樣的隱晦,以便儘可能簡化此簡短示例。

這裏有一個簡短的用法例如:

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 
+0

感謝您回答我正在尋找的答謝。一個問題,它需要ActiveSupport?謝謝。 – 2009-04-23 04:57:25

+0

它使用`Hash#blank?`(在`valid?`方法中)。但是就是這樣,嘿。不應該太難以放棄active_support。我只是假定它已經在那裏了,因爲你無論如何都處在rails的環境中。 – 2009-04-23 07:44:02

2

你想Validatablesudo gem install validatable

class Person 
    include Validatable 
    validates_presence_of :name 
    attr_accessor :name 
end 

而且,是Validatable上沒有的ActiveSupport的依賴。

相關問題