class InputSanitizer::V2::Types::SortByCheck

Public Instance Methods

call(value, options = {}) click to toggle source
# File lib/input_sanitizer/v2/types.rb, line 182
def call(value, options = {})
  check_options!(options)

  key, direction = split(value)
  direction = 'asc' if direction.blank?

  # special case when fallback takes care of separator sanitization e.g. custom fields
  if options[:fallback] && !allowed_directions.include?(direction)
    direction = 'asc'
    key = value
  end

  unless valid?(key, direction, options)
    raise InputSanitizer::ValueNotAllowedError.new(value)
  end

  [key, direction]
end

Private Instance Methods

allowed_directions() click to toggle source
# File lib/input_sanitizer/v2/types.rb, line 223
def allowed_directions
  ['asc', 'desc']
end
check_options!(options) click to toggle source
# File lib/input_sanitizer/v2/types.rb, line 216
def check_options!(options)
  fallback = options[:fallback]
  if fallback && !fallback.respond_to?(:call)
    raise ArgumentError, ":fallback option must respond to method :call (proc, lambda etc)"
  end
end
split(value) click to toggle source
# File lib/input_sanitizer/v2/types.rb, line 211
def split(value)
  head, _, tail = value.to_s.rpartition(':')
  head.empty? ? [tail, head] : [head, tail]
end
valid?(key, direction, options) click to toggle source
# File lib/input_sanitizer/v2/types.rb, line 202
def valid?(key, direction, options)
  allowed_keys = options[:allow]
  fallback = options[:fallback]

  allowed_directions.include?(direction) &&
    ((allowed_keys && allowed_keys.include?(key)) ||
      (fallback && fallback.call(key, direction, options)))
end