module MCollective::Validator

Public Class Methods

[](klass) click to toggle source

Returns and instance of the Plugin class from which objects can be created. Valid plugin names are

:valplugin
"valplugin"
"ValpluginValidator"
   # File lib/mcollective/validator.rb
24 def self.[](klass)
25   if klass.is_a?(Symbol)
26     klass = validator_class(klass)
27   elsif !(klass.match(/.*Validator$/))
28     klass = validator_class(klass)
29   end
30 
31   const_get(klass)
32 end
has_validator?(validator) click to toggle source
   # File lib/mcollective/validator.rb
43 def self.has_validator?(validator)
44   const_defined?(validator_class(validator))
45 end
load_validators() click to toggle source

Loads the validator plugins. Validators will only be loaded every 5 minutes

   # File lib/mcollective/validator.rb
 7 def self.load_validators
 8   begin
 9     @@validator_mutex.lock
10     if load_validators?
11       @last_load = Time.now.to_i
12       PluginManager.find_and_load("validator")
13     end
14   ensure
15     @@validator_mutex.unlock
16   end
17 end
load_validators?() click to toggle source
   # File lib/mcollective/validator.rb
51 def self.load_validators?
52   return true if @last_load.nil?
53   (@last_load - Time.now.to_i) > 300
54 end
method_missing(method, *args, &block) click to toggle source

Allows validation plugins to be called like module methods : Validator.validate()

   # File lib/mcollective/validator.rb
35 def self.method_missing(method, *args, &block)
36   if has_validator?(method)
37     validator = Validator[method].validate(*args)
38   else
39     raise ValidatorError, "Unknown validator: '#{method}'."
40   end
41 end
validate(validator, validation) click to toggle source

Generic validate method that will call the correct validator plugin based on the type of the validation parameter

   # File lib/mcollective/validator.rb
58 def self.validate(validator, validation)
59   Validator.load_validators
60 
61   begin
62     if [:integer, :boolean, :float, :number, :string].include?(validation)
63       Validator.typecheck(validator, validation)
64 
65     else
66       case validation
67         when Regexp,String
68           Validator.regex(validator, validation)
69 
70         when Symbol
71           Validator.send(validation, validator)
72 
73         when Array
74           Validator.array(validator, validation)
75 
76         when Class
77           Validator.typecheck(validator, validation)
78       end
79     end
80   rescue => e
81     raise ValidatorError, e.to_s
82   end
83 end
validator_class(validator) click to toggle source
   # File lib/mcollective/validator.rb
47 def self.validator_class(validator)
48   "#{validator.to_s.capitalize}Validator"
49 end