module Rekiq::Validator

Constants

NUMERIC_OPTIONS

Public Class Methods

included(base) click to toggle source
# File lib/rekiq/validator.rb, line 19
def self.included(base)
  base.extend(ClassMethods)
  base.for_validation = []
end

Public Instance Methods

validate!() click to toggle source
# File lib/rekiq/validator.rb, line 28
def validate!
  self.class.for_validation.each do |v|
    attribute_name = v[:attribute_name]
    type           = v[:type]
    options        = v[:options]
    value          = instance_variable_get("@#{attribute_name}")

    unless options[:allow_nil] and value.nil?
      send("validate_#{type}!", attribute_name, value, options)
    end
  end
end
validate_bool!(attribute_name, value, options) click to toggle source
# File lib/rekiq/validator.rb, line 55
def validate_bool!(attribute_name, value, options)
  unless [true, false].include?(value)
    raise InvalidAttributeValue, "#{attribute_name} must be either true " \
                                 'or false'
  end
end
validate_numeric!(attribute_name, value, options) click to toggle source
# File lib/rekiq/validator.rb, line 41
def validate_numeric!(attribute_name, value, options)
  unless value.is_a?(Numeric)
    raise InvalidAttributeValue, "#{attribute_name} must be numeric"
  end

  options.each do |key, option_value|
    if NUMERIC_OPTIONS.key?(key) and
       !value.send(NUMERIC_OPTIONS[key], option_value)
      raise InvalidAttributeValue, "#{attribute_name} must be greater " \
                                   "or equal to #{option_value}"
    end
  end
end
validate_schedule!(attribute_name, value, options) click to toggle source
# File lib/rekiq/validator.rb, line 62
def validate_schedule!(attribute_name, value, options)
  unless value.respond_to?(:next_occurrence) and
         value.method(:next_occurrence).arity.abs == 1
    raise InvalidConf, "#{attribute_name} must respond to next_occurrence " \
                       'and receive one argument of type Time'
  end
end