Module: MCollective::Validator
- Defined in:
- lib/mcollective/validator.rb
Class Method Summary collapse
-
.[](klass) ⇒ Object
Returns and instance of the Plugin class from which objects can be created.
- .has_validator?(validator) ⇒ Boolean
-
.load_validators ⇒ Object
Loads the validator plugins.
- .load_validators? ⇒ Boolean
-
.method_missing(method, *args, &block) ⇒ Object
Allows validation plugins to be called like module methods : Validator.validate().
-
.validate(validator, validation) ⇒ Object
Generic validate method that will call the correct validator plugin based on the type of the validation parameter.
- .validator_class(validator) ⇒ Object
Class Method Details
.[](klass) ⇒ Object
Returns and instance of the Plugin class from which objects can be created. Valid plugin names are
:valplugin
"valplugin"
"ValpluginValidator"
18 19 20 21 22 23 24 25 26 |
# File 'lib/mcollective/validator.rb', line 18 def self.[](klass) if klass.is_a?(Symbol) klass = validator_class(klass) elsif !(klass.match(/.*Validator$/)) klass = validator_class(klass) end const_get(klass) end |
.has_validator?(validator) ⇒ Boolean
37 38 39 |
# File 'lib/mcollective/validator.rb', line 37 def self.has_validator?(validator) const_defined?(validator_class(validator)) end |
.load_validators ⇒ Object
Loads the validator plugins. Validators will only be loaded every 5 minutes
6 7 8 9 10 11 |
# File 'lib/mcollective/validator.rb', line 6 def self.load_validators if load_validators? @last_load = Time.now.to_i PluginManager.find_and_load("validator") end end |
.load_validators? ⇒ Boolean
45 46 47 48 49 |
# File 'lib/mcollective/validator.rb', line 45 def self.load_validators? return true if @last_load.nil? (@last_load - Time.now.to_i) > 300 end |
.method_missing(method, *args, &block) ⇒ Object
Allows validation plugins to be called like module methods : Validator.validate()
29 30 31 32 33 34 35 |
# File 'lib/mcollective/validator.rb', line 29 def self.method_missing(method, *args, &block) if has_validator?(method) validator = Validator[method].validate(*args) else raise ValidatorError, "Unknown validator: '#{method}'." end end |
.validate(validator, validation) ⇒ Object
Generic validate method that will call the correct validator plugin based on the type of the validation parameter
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mcollective/validator.rb', line 53 def self.validate(validator, validation) Validator.load_validators begin if [:integer, :boolean, :float, :number, :string].include?(validation) Validator.typecheck(validator, validation) else case validation when Regexp,String Validator.regex(validator, validation) when Symbol Validator.send(validation, validator) when Array Validator.array(validator, validation) when Class Validator.typecheck(validator, validation) end end rescue => e raise ValidatorError, e.to_s end end |
.validator_class(validator) ⇒ Object
41 42 43 |
# File 'lib/mcollective/validator.rb', line 41 def self.validator_class(validator) "#{validator.to_s.capitalize}Validator" end |