Class: ActiveRecord::Refined::AST::Like

Inherits:
Predicate show all
Defined in:
lib/active_record/refined/ast.rb

Constant Summary collapse

ESCAPE =
"\\".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Predicate

#!, #&, #|

Methods inherited from Node

#as, #asc, #desc

Constructor Details

#initialize(operand, pattern, escape = nil) ⇒ Like

Returns a new instance of Like.



248
249
250
251
252
# File 'lib/active_record/refined/ast.rb', line 248

def initialize(operand, pattern, escape = nil)
  @operand = operand
  @pattern = pattern
  @escape = escape
end

Instance Attribute Details

#escapeObject (readonly)

Returns the value of attribute escape.



246
247
248
# File 'lib/active_record/refined/ast.rb', line 246

def escape
  @escape
end

#operandObject (readonly)

Returns the value of attribute operand.



246
247
248
# File 'lib/active_record/refined/ast.rb', line 246

def operand
  @operand
end

#patternObject (readonly)

Returns the value of attribute pattern.



246
247
248
# File 'lib/active_record/refined/ast.rb', line 246

def pattern
  @pattern
end

Class Method Details

.escape(string) ⇒ Object

Escapes % and _ so that they match literally. The pattern built from the result must be used with ESCAPE, since SQLite has no default escape character.



242
243
244
# File 'lib/active_record/refined/ast.rb', line 242

def self.escape(string)
  ActiveRecord::Base.sanitize_sql_like(string, ESCAPE)
end

Instance Method Details

#to_arel(table) ⇒ Object



254
255
256
257
258
# File 'lib/active_record/refined/ast.rb', line 254

def to_arel(table)
  # Arel matches case-insensitively unless told otherwise, which turns
  # into ILIKE on PostgreSQL. like? means SQL LIKE on every adapter.
  to_arel_operand(operand, table).matches(pattern, escape, true)
end