Module: MCollective::Validator
- Defined in:
- lib/mcollective/validator.rb,
lib/mcollective/validator/array_validator.rb,
lib/mcollective/validator/regex_validator.rb,
lib/mcollective/validator/length_validator.rb,
lib/mcollective/validator/shellsafe_validator.rb,
lib/mcollective/validator/typecheck_validator.rb,
lib/mcollective/validator/ipv4address_validator.rb,
lib/mcollective/validator/ipv6address_validator.rb
Defined Under Namespace
Classes: ArrayValidator, Ipv4addressValidator, Ipv6addressValidator, LengthValidator, RegexValidator, ShellsafeValidator, TypecheckValidator
Constant Summary collapse
- @@validator_mutex =
Mutex.new
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"
24 25 26 27 28 29 30 31 32 |
# File 'lib/mcollective/validator.rb', line 24 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
43 44 45 |
# File 'lib/mcollective/validator.rb', line 43 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
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/mcollective/validator.rb', line 7 def self.load_validators begin @@validator_mutex.lock if load_validators? @last_load = Time.now.to_i PluginManager.find_and_load("validator") end ensure @@validator_mutex.unlock end end |
.load_validators? ⇒ Boolean
51 52 53 54 |
# File 'lib/mcollective/validator.rb', line 51 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()
35 36 37 38 39 40 41 |
# File 'lib/mcollective/validator.rb', line 35 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mcollective/validator.rb', line 58 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
47 48 49 |
# File 'lib/mcollective/validator.rb', line 47 def self.validator_class(validator) "#{validator.to_s.capitalize}Validator" end |