Class: Wallaby::ActiveRecord::ModelServiceProvider::Querier::Escaper

Inherits:
Object
  • Object
show all
Includes:
ActiveRecord::Sanitization
Defined in:
lib/adapters/wallaby/active_record/model_service_provider/querier/escaper.rb

Overview

Build up query using the results

Constant Summary collapse

LIKE_SIGN =

:nodoc:

/[%_]/.freeze
PCT =

:nodoc:

'%'

Class Method Summary collapse

Class Method Details

.execute(keyword) ⇒ String

Returns escaped string for LIKE query.

Examples:

Return the escaped keyword if the first/last char of the keyword is ‘%`/`_`

Wallaby::ActiveRecord::ModelServiceProvider::Querier::Escaper.execute('%something_else%')
# => '%something\_else%'

Return the escaped keyword wrapped with ‘%` if the first/last char of the keyword is NOT `%`/`_`

Wallaby::ActiveRecord::ModelServiceProvider::Querier::Escaper.execute('keyword')
# => '%keyword%'

Parameters:

  • keyword (String)

Returns:

  • (String)

    escaped string for LIKE query



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/adapters/wallaby/active_record/model_service_provider/querier/escaper.rb', line 22

def execute(keyword)
  first = keyword.first
  last = keyword.last
  start_with, start_index = LIKE_SIGN.match?(first) ? [true, 1] : [false, 0]
  end_with, end_index = LIKE_SIGN.match?(last) ? [true, -2] : [false, -1]
  escaped = sanitize_sql_like keyword[start_index..end_index]
  starting = sign(start_with, first, end_with)
  ending = sign(end_with, last, start_with)

  "#{starting}#{escaped}#{ending}"
end