class Cuprum::Collections::Errors::UnknownOperator

An error returned when a query attempts to filter by an unknown operator.

Constants

TYPE

Short string used to identify the type of error.

Attributes

operator[R]

@return [String, Symbol] the unknown operator.

Public Class Methods

new(operator:) click to toggle source

@param operator [String, Symbol] The unknown operator.

Calls superclass method
# File lib/cuprum/collections/errors/unknown_operator.rb, line 13
def initialize(operator:)
  @operator = operator

  super(
    message:  generate_message,
    operator: operator
  )
end

Public Instance Methods

as_json() click to toggle source

@return [Hash] a serializable hash representation of the error.

# File lib/cuprum/collections/errors/unknown_operator.rb, line 26
def as_json
  {
    'data'    => {
      'corrections' => corrections,
      'operator'    => operator
    },
    'message' => message,
    'type'    => type
  }
end
corrections() click to toggle source

@return [Array<String>] Suggested possible values for the operator.

# File lib/cuprum/collections/errors/unknown_operator.rb, line 38
def corrections
  @corrections ||=
    DidYouMean::SpellChecker
      .new(dictionary: Cuprum::Collections::Queries::VALID_OPERATORS.to_a)
      .correct(operator)
end
type() click to toggle source

@return [String] short string used to identify the type of error.

# File lib/cuprum/collections/errors/unknown_operator.rb, line 46
def type
  TYPE
end

Private Instance Methods

generate_message() click to toggle source
# File lib/cuprum/collections/errors/unknown_operator.rb, line 52
def generate_message
  message = "unknown operator #{operator.inspect}"

  return message if corrections.empty?

  "#{message} - did you mean #{suggestion}?"
end
suggestion() click to toggle source
# File lib/cuprum/collections/errors/unknown_operator.rb, line 60
def suggestion
  tools.ary.humanize_list(
    corrections.map(&:inspect),
    last_separator: ', or '
  )
end
tools() click to toggle source
# File lib/cuprum/collections/errors/unknown_operator.rb, line 67
def tools
  SleepingKingStudios::Tools::Toolbelt.instance
end