Module: CanCamel::Validators
- Included in:
- Filters
- Defined in:
- lib/can_camel/validators.rb
Constant Summary collapse
- ValidationError =
Class.new(StandardError)
- UnknownFilter =
Class.new(ValidationError)
Class Method Summary collapse
- .included(base) ⇒ Object
- .validate!(filter:, args:) ⇒ Object
- .validates(filter, field = nil, **args) ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 7 8 |
# File 'lib/can_camel/validators.rb', line 6 def included(base) base.define_singleton_method(:validates) { |*args| CanCamel::Validators.validates *args } end |
.validate!(filter:, args:) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/can_camel/validators.rb', line 23 def validate!(filter:, args:) filter = filter.to_sym unless validators[filter] raise UnknownFilter unless CanCamel::Filters.respond_to? filter return end validators[filter].each { |x| x.call(args) } end |
.validates(filter, field = nil, **args) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/can_camel/validators.rb', line 10 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 |