5 # Loads the validator plugins. Validators will only be loaded every 5 minutes
6 def self.load_validators
8 @last_load = Time.now.to_i
9 PluginManager.find_and_load("validator")
13 # Returns and instance of the Plugin class from which objects can be created.
14 # Valid plugin names are
17 # "ValpluginValidator"
19 if klass.is_a?(Symbol)
20 klass = validator_class(klass)
21 elsif !(klass.match(/.*Validator$/))
22 klass = validator_class(klass)
28 # Allows validation plugins to be called like module methods : Validator.validate()
29 def self.method_missing(method, *args, &block)
30 if has_validator?(method)
31 validator = Validator[method].validate(*args)
33 raise ValidatorError, "Unknown validator: '#{method}'."
37 def self.has_validator?(validator)
38 const_defined?(validator_class(validator))
41 def self.validator_class(validator)
42 "#{validator.to_s.capitalize}Validator"
45 def self.load_validators?
46 return true if @last_load.nil?
48 (@last_load - Time.now.to_i) > 300
51 # Generic validate method that will call the correct validator
52 # plugin based on the type of the validation parameter
53 def self.validate(validator, validation)
54 Validator.load_validators
57 if [:integer, :boolean, :float, :number, :string].include?(validation)
58 Validator.typecheck(validator, validation)
63 Validator.regex(validator, validation)
66 Validator.send(validation, validator)
69 Validator.array(validator, validation)
72 Validator.typecheck(validator, validation)
76 raise ValidatorError, e.to_s