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, 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) click to toggle source
# File lib/mcollective/validator.rb, line 37
def self.has_validator?(validator)
  const_defined?(validator_class(validator))
end
load_validators() click to toggle source

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

# 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?() click to toggle source
# 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) click to toggle source

Allows validation plugins to be called like module methods : ::validate

# 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) 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, 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) click to toggle source
# File lib/mcollective/validator.rb, line 41
def self.validator_class(validator)
  "#{validator.to_s.capitalize}Validator"
end