Class: ActiveRecord::Refined::AST::Like
- Defined in:
- lib/active_record/refined/ast.rb
Constant Summary collapse
- ESCAPE =
"\\".freeze
Instance Attribute Summary collapse
-
#escape ⇒ Object
readonly
Returns the value of attribute escape.
-
#operand ⇒ Object
readonly
Returns the value of attribute operand.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Class Method Summary collapse
-
.any(operand, patterns) ⇒ Object
ORs one LIKE per pattern, for the shortcuts that accept several literals the way String#start_with? does.
-
.escape(string) ⇒ Object
Escapes % and _ so that they match literally.
Instance Method Summary collapse
-
#initialize(operand, pattern, escape = nil) ⇒ Like
constructor
A new instance of Like.
- #to_arel(table) ⇒ Object
Methods inherited from Predicate
Methods inherited from Node
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
#escape ⇒ Object (readonly)
Returns the value of attribute escape.
313 314 315 |
# File 'lib/active_record/refined/ast.rb', line 313 def escape @escape end |
#operand ⇒ Object (readonly)
Returns the value of attribute operand.
313 314 315 |
# File 'lib/active_record/refined/ast.rb', line 313 def operand @operand end |
#pattern ⇒ Object (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 |