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.



609
610
611
612
613
614
615
616
# File 'lib/active_record/refined/ast.rb', line 609

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.



607
608
609
# File 'lib/active_record/refined/ast.rb', line 607

def case_sensitive
  @case_sensitive
end

#escapeObject (readonly)

Returns the value of attribute escape.



607
608
609
# File 'lib/active_record/refined/ast.rb', line 607

def escape
  @escape
end

#negatedObject (readonly)

Returns the value of attribute negated.



607
608
609
# File 'lib/active_record/refined/ast.rb', line 607

def negated
  @negated
end

#operandObject (readonly)

Returns the value of attribute operand.



607
608
609
# File 'lib/active_record/refined/ast.rb', line 607

def operand
  @operand
end

#patternObject (readonly)

Returns the value of attribute pattern.



607
608
609
# File 'lib/active_record/refined/ast.rb', line 607

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.



602
603
604
605
# File 'lib/active_record/refined/ast.rb', line 602

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.



596
597
598
# File 'lib/active_record/refined/ast.rb', line 596

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

Instance Method Details

#to_arel(table) ⇒ Object



618
619
620
621
622
623
624
# File 'lib/active_record/refined/ast.rb', line 618

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