class Digget::Validator

This class is the validator superclass containing the logic to cast the parameters and verify the options that are defined on them

Attributes

casted_params[R]

This method returns a hash with the parameters casted to their requested type @return [Hash] A hash with as a key the parameter symbol and the casted value as value

errors[R]

This method returns an array with all the errors during validation @return [Array] An array containing the validation errors

Public Class Methods

new(params, object = nil) click to toggle source
# File lib/digget/validator.rb, line 7
def initialize(params, object = nil)
  @object = object
  @casted_params = {}
  @errors = []
  @params = params
end

Public Instance Methods

filter(name, type, options = {}) click to toggle source
# File lib/digget/validator.rb, line 34
def filter(name, type, options = {})
  param = verify(name, type, options)
  return object unless @errors.empty?
  filter_options(param, name, options)
end
valid?() click to toggle source

This method is a helper method to determine whether the validation of the parameters was successful @return `true` if there were no errors during parameter checking, `false` otherwise

# File lib/digget/validator.rb, line 42
def valid?
  @errors.empty?
end
verify(name, type, options = {}) click to toggle source

This method verifies one of the parameters with the provided options @param name [Symbol] The name of the param in the `params` hash @param type [Class] Type that the parameter should be casted to @param options [Hash] Validation options for this parameter

# File lib/digget/validator.rb, line 26
def verify(name, type, options = {})
  casted_param = cast(@params[name], type)
  return unless @errors.empty?
  @casted_params[name] = casted_param
  verify_options(casted_param, name, options)
  casted_param
end

Private Instance Methods

cast(param, type) click to toggle source

This method casts the parameter to the requested type if possible @param param [String] Parameter that is about to get casted @param type [Class] The type that the parameter should be after this method @return Returns a casted version of the provided parameter

# File lib/digget/validator.rb, line 52
def cast(param, type)
  if param.nil?
    return nil
  elsif type == Integer
    return Integer(param)
  elsif type == String
    return String(param)
  elsif type == Float
    return Float(param)
  elsif type == Date
    return Date.parse(param)
  elsif type == Time
    return Time.parse(param)
  end
rescue ArgumentError
  errors.append(I18n.t('digget.cast', param: param, type: type))
end
filter_options(param, name, options = {}) click to toggle source
# File lib/digget/validator.rb, line 105
def filter_options(param, name, options = {})
  name = options.key?(:column) ? options[:column] : name
  return unless @object && options.key?(:filter)
  filter = options[:filter]
  if filter == :min
    @object = @object.where(@object.arel_table[name].gteq(param))
  elsif filter == :max
    @object = @object.where(@object.arel_table[name].lteq(param))
  elsif filter == :equal
    @object = @object.where(@object.arel_table[name].eq(param))
  elsif filter == :not_equal
    @object = @object.where(@object.arel_table[name].not_eq(param))
  end
end
verify_options(param, name, options = {}) click to toggle source

This method verifies the parameters and adds errors when a parameter doesn't conform with the provided verification options @param param The parameter value that needs to be verified @param name [String] The name of the parameter, used to be displayed in error messages @param options [Hash] The verification options for the provided parameter

# File lib/digget/validator.rb, line 75
def verify_options(param, name, options = {})
  options.each do |key, value|
    if key == :required && value
      next unless param.nil?
      @errors.append(I18n.t('digget.required', name: name))
    elsif key == :min && !param.nil?
      next unless param < value
      @errors.append(I18n.t('digget.min', name: name, value: value))
    elsif key == :max && !param.nil?
      next unless param > value
      @errors.append(I18n.t('digget.max', name: name, value: value))
    elsif key == :equal && !param.nil
      next unless param != value
      @errors.append(I18n.t('digget.equal', name: name, value: value))
    elsif key == :not_equal && !param.nil
      next unless param == value
      @errors.append(I18n.t('digget.not_equal', name: name, value: value))
    elsif key == :min_length && !param.nil?
      next unless param.length < value
      @errors.append(I18n.t('digget.min_length', name: name, value: value))
    elsif key == :max_length && !param.nil?
      next unless param.length > value
      @errors.append(I18n.t('digget.max_length', name: name, value: value))
    elsif key == :length && !param.nil?
      next unless param.length == value
      @errors.append(I18n.t('digget.length', name: name, value: value))
    end
  end
end