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, case_sensitive: true, negated: false) ⇒ Like

Returns a new instance of Like.



1293
1294
1295
1296
1297
1298
1299
1300
# File 'lib/active_record/refined/ast.rb', line 1293

def initialize(operand, pattern, escape = nil, case_sensitive: true,
               negated: false)
  @operand = operand
  @pattern = pattern
  @escape = escape
  @case_sensitive = case_sensitive
  @negated = negated
end

Instance Attribute Details

#case_sensitiveObject (readonly)

Returns the value of attribute case_sensitive.



1291
1292
1293
# File 'lib/active_record/refined/ast.rb', line 1291

def case_sensitive
  @case_sensitive
end

#escapeObject (readonly)

Returns the value of attribute escape.



1291
1292
1293
# File 'lib/active_record/refined/ast.rb', line 1291

def escape
  @escape
end

#negatedObject (readonly)

Returns the value of attribute negated.



1291
1292
1293
# File 'lib/active_record/refined/ast.rb', line 1291

def negated
  @negated
end

#operandObject (readonly)

Returns the value of attribute operand.



1291
1292
1293
# File 'lib/active_record/refined/ast.rb', line 1291

def operand
  @operand
end

#patternObject (readonly)

Returns the value of attribute pattern.



1291
1292
1293
# File 'lib/active_record/refined/ast.rb', line 1291

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.



1286
1287
1288
1289
# File 'lib/active_record/refined/ast.rb', line 1286

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.



1280
1281
1282
# File 'lib/active_record/refined/ast.rb', line 1280

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

Instance Method Details

#to_arel(table, model) ⇒ Object



1302
1303
1304
1305
1306
1307
1308
# File 'lib/active_record/refined/ast.rb', line 1302

def to_arel(table, model)
  # Arel matches case-insensitively unless told otherwise, which is
  # what picks ILIKE over LIKE on PostgreSQL.
  to_arel_operand(operand, table, model).
    public_send(negated ? :does_not_match : :matches,
                pattern, escape, case_sensitive)
end