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

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

Constant Summary collapse

ESCAPE =
"\\"

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.



2024
2025
2026
2027
2028
2029
2030
2031
# File 'lib/active_record/refined/ast.rb', line 2024

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.



2022
2023
2024
# File 'lib/active_record/refined/ast.rb', line 2022

def case_sensitive
  @case_sensitive
end

#escapeObject (readonly)

Returns the value of attribute escape.



2022
2023
2024
# File 'lib/active_record/refined/ast.rb', line 2022

def escape
  @escape
end

#negatedObject (readonly)

Returns the value of attribute negated.



2022
2023
2024
# File 'lib/active_record/refined/ast.rb', line 2022

def negated
  @negated
end

#operandObject (readonly)

Returns the value of attribute operand.



2022
2023
2024
# File 'lib/active_record/refined/ast.rb', line 2022

def operand
  @operand
end

#patternObject (readonly)

Returns the value of attribute pattern.



2022
2023
2024
# File 'lib/active_record/refined/ast.rb', line 2022

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.



2017
2018
2019
2020
# File 'lib/active_record/refined/ast.rb', line 2017

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.



2011
2012
2013
# File 'lib/active_record/refined/ast.rb', line 2011

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

Instance Method Details

#to_arel(table, model) ⇒ Object



2033
2034
2035
2036
2037
2038
2039
# File 'lib/active_record/refined/ast.rb', line 2033

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