Class: ActiveRecord::Extensions::RegexpBase

Inherits:
Object
  • Object
show all
Defined in:
lib/ar-extensions/extensions.rb

Overview

A base class for database vendor specific Regexp implementations. This is meant to be subclassed only because of the helper method(s) it provides.

Defined Under Namespace

Classes: RegexpResult

Constant Summary collapse

NOT_EQUAL_RGX =
/^(.+)_(ne|not|does_not_match)$/
EQUAL_RGX =
/^(.+)_(eq|is|matches)$/

Class Method Summary collapse

Class Method Details

.field_result(str, caller) ⇒ Object

Given the passed in str and caller this will return a RegexpResult object which gives the database quoted fieldname/column and can tell you whether or not the original str is indicating a negated regular expression.

Examples

r = RegexpBase.field_result( 'id' )
r.fieldname => # 'id'
r.negate?   => # false

r = RegexpBase.field_result( 'id_ne' )
r.fieldname => # 'id'
r.negate?   => # true

r = RegexpBase.field_result( 'id_not' )
r.fieldname => # 'id'
r.negate?   => # true

r = RegexpBase.field_result( 'id_does_not_match' )
r.fieldname => # 'id'
r.negate?   => # true


372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/ar-extensions/extensions.rb', line 372

def self.field_result( str, caller )
  negate = false
  if match_data=str.to_s.match( NOT_EQUAL_RGX )
    negate = true
    str = match_data.captures[0]
  end
  if match_data=str.to_s.match( EQUAL_RGX )
    str = match_data.captures[0]
  end
  fieldname = caller.connection.quote_column_name( str )
  RegexpResult.new( fieldname, negate )
end