Class: Lafcadio::Query::Like

Inherits:
Condition show all
Defined in:
lib/lafcadio/query.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from Condition

#domain_class

Instance Method Summary collapse

Methods inherited from Condition

#&, #db_field_name, #eql?, #field, #implies?, #not, #one_pk_id?, #primary_key_field?, #query, search_term_type, #to_condition, #|

Constructor Details

#initialize(fieldName, searchTerm, domain_class, matchType = :pre_and_post) ⇒ Like

Returns a new instance of Like.



623
624
625
626
627
628
629
630
631
632
# File 'lib/lafcadio/query.rb', line 623

def initialize(
	fieldName, searchTerm, domain_class, matchType = :pre_and_post
)
	if searchTerm.is_a? Regexp
		searchTerm = process_regexp searchTerm
	else
		@matchType = matchType
	end
	super fieldName, searchTerm, domain_class
end

Instance Method Details

#dobj_satisfies?(anObj) ⇒ Boolean

Returns:

  • (Boolean)


634
635
636
637
638
639
640
641
642
# File 'lib/lafcadio/query.rb', line 634

def dobj_satisfies?(anObj)
	value = anObj.send @fieldName
	value = value.pk_id.to_s if value.respond_to?( :pk_id )
	if value.is_a?( Array )
		value.include? @searchTerm
	else
		!regexp.match( value ).nil?
	end
end

#process_regexp(searchTerm) ⇒ Object



644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/lafcadio/query.rb', line 644

def process_regexp( searchTerm )
	if searchTerm.source =~ /^\^(.*)/
		@matchType = :post_only
		$1
	elsif searchTerm.source =~ /(.*)\$$/
		@matchType = :pre_only
		$1
	else
		@matchType = :pre_and_post
		searchTerm.source
	end
end

#regexpObject



657
658
659
660
661
662
663
664
665
# File 'lib/lafcadio/query.rb', line 657

def regexp
	if @matchType == :pre_and_post
		Regexp.new( @searchTerm, Regexp::IGNORECASE )
	elsif @matchType == :pre_only
		Regexp.new( @searchTerm.to_s + "$", Regexp::IGNORECASE )
	elsif @matchType == :post_only
		Regexp.new( "^" + @searchTerm, Regexp::IGNORECASE )
	end
end

#to_sqlObject



667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/lafcadio/query.rb', line 667

def to_sql
	withWildcards = @searchTerm.clone
	if @matchType == :pre_and_post
		withWildcards = "%" + withWildcards + "%"
	elsif @matchType == :pre_only
		withWildcards = "%" + withWildcards
	elsif @matchType == :post_only
		withWildcards += "%"
	end
	withWildcards.gsub!( /(\\?\.)/ ) { |m| m.size == 1 ? "_" : "." }
	"#{ db_field_name } like '#{ withWildcards }'"
end