Module: Rekiq::Validator

Included in:
Configuration, Contract
Defined in:
lib/rekiq/validator.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

NUMERIC_OPTIONS =
{
  greater_than_or_equal_to: :>=
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



19
20
21
22
# File 'lib/rekiq/validator.rb', line 19

def self.included(base)
  base.extend(ClassMethods)
  base.for_validation = []
end

Instance Method Details

#validate!Object



28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



55
56
57
58
59
60
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



62
63
64
65
66
67
68
# 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