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.



315
316
317
318
319
# File 'lib/active_record/refined/ast.rb', line 315

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

Instance Attribute Details

#escapeObject (readonly)

Returns the value of attribute escape.



313
314
315
# File 'lib/active_record/refined/ast.rb', line 313

def escape
  @escape
end

#operandObject (readonly)

Returns the value of attribute operand.



313
314
315
# File 'lib/active_record/refined/ast.rb', line 313

def operand
  @operand
end

#patternObject (readonly)

Returns the value of attribute pattern.



313
314
315
# File 'lib/active_record/refined/ast.rb', line 313

def pattern
  @pattern
end

Class Method Details

.any(operand, patterns) ⇒ Object

ORs one LIKE per pattern, for the shortcuts that accept several literals the way String#start_with? does.



308
309
310
311
# File 'lib/active_record/refined/ast.rb', line 308

def self.any(operand, patterns)
  patterns.map {|pattern| new(operand, pattern, ESCAPE) }.
    inject {|left, right| Or.new(left, right) }
end

.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.



302
303
304
# File 'lib/active_record/refined/ast.rb', line 302

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

Instance Method Details

#to_arel(table) ⇒ Object



321
322
323
324
325
# File 'lib/active_record/refined/ast.rb', line 321

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