module CanCamel::Validators

Constants

SUBMODULES
UnknownFilter
ValidationError

Public Class Methods

included(base) click to toggle source
# File lib/can_camel/validators.rb, line 12
def included(base)
  base.define_singleton_method(:validates) { |*args| CanCamel::Validators.validates *args }
end
validate!(filter:, args:, path: nil) click to toggle source
# File lib/can_camel/validators.rb, line 29
def validate!(filter:, args:, path: nil)
  filter = filter.to_sym
  unless validators[filter]
    raise UnknownFilter unless CanCamel::Filter.filters.include? filter
    return
  end
  validators[filter].each { |x| x.call(path: path, **args) }
end
validates(filter, field = nil, **args) click to toggle source
# File lib/can_camel/validators.rb, line 16
def validates(filter, field = nil, **args)
  validators[filter] ||= []
  args.each do |key, value|
    validator =
      if field
        -> (x) { send(key, field: x[field], **value) }
      else
        -> (x) { send(key, args: x, **value) }
      end
    validators[filter].push(validator)
  end
end

Private Class Methods

validators() click to toggle source
# File lib/can_camel/validators.rb, line 40
def validators
  @validators ||= {}
end