Class: Lafcadio::Query::Like

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

Overview

:nodoc:

Constant Summary collapse

PRE_AND_POST =
1
PRE_ONLY =
2
POST_ONLY =
3

Instance Attribute Summary

Attributes inherited from Condition

#objectType

Instance Method Summary collapse

Methods inherited from Condition

#dbFieldName, #getField, #not, #primaryKeyField?, searchTermType

Constructor Details

#initialize(fieldName, searchTerm, objectType, matchType = PRE_AND_POST) ⇒ Like

Returns a new instance of Like.



10
11
12
13
14
# File 'lib/lafcadio/query/Like.rb', line 10

def initialize(
		fieldName, searchTerm, objectType, matchType = PRE_AND_POST)
	super fieldName, searchTerm, objectType
	@matchType = matchType
end

Instance Method Details

#objectMeets(anObj) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lafcadio/query/Like.rb', line 28

def objectMeets(anObj)
	value = anObj.send @fieldName
	if value.class <= DomainObject || value.class == DomainObjectProxy
		value = value.pkId.to_s
	end
	if value.class <= Array
		(value.index(@searchTerm) != nil)
	else
		if @matchType == PRE_AND_POST
			regexp = Regexp.new(@searchTerm)
		elsif @matchType == PRE_ONLY
			regexp = Regexp.new(@searchTerm.to_s + "$")
		elsif @matchType == POST_ONLY
			regexp = Regexp.new("^" + @searchTerm)
		end
		regexp.match(value) != nil
	end
end

#toSqlObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lafcadio/query/Like.rb', line 16

def toSql
	withWildcards = @searchTerm
	if @matchType == PRE_AND_POST
		withWildcards = "%" + withWildcards + "%"
	elsif @matchType == PRE_ONLY
		withWildcards = "%" + withWildcards
	elsif @matchType == POST_ONLY
		withWildcards += "%"
	end
	"#{ dbFieldName } like '#{ withWildcards }'"
end