Class: ActiveRecord::Extensions::SqliteRegexp

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

Overview

ActiveRecord::Extension for implementing Regexp implementation for MySQL. See documention for RegexpBase.

Constant Summary

Constants inherited from RegexpBase

RegexpBase::NOT_EQUAL_RGX

Class Method Summary collapse

Methods inherited from RegexpBase

field_result

Class Method Details

.add_rlike_function(connection) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/ar-extensions/extensions.rb', line 430

def self.add_rlike_function( connection )
  self.connections << connection 
  unless connection.respond_to?( 'sqlite_regexp_support?' )
    class << connection
      def sqlite_regexp_support? ; true ; end
    end
    connection.instance_eval( '@connection' ).create_function( 'rlike', 3 ) do |func, a, b, negate|
      if negate =~ /true/
        func.set_result 1 if a.to_s !~ /#{b}/
      else
        func.set_result 1 if a.to_s =~ /#{b}/
      end
    end
  end
end

.process(key, val, caller) ⇒ Object



446
447
448
449
450
451
452
453
# File 'lib/ar-extensions/extensions.rb', line 446

def self.process( key, val, caller )
  return nil unless val.is_a?( Regexp )
  r = field_result( key, caller )
  unless self.connections.include?( caller.connection )
    add_rlike_function( caller.connection )
  end
  Result.new( "rlike( #{r.fieldname}, ?, '#{r.negate?}' )", val )
end