Class: Lafcadio::Query::Compare

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

Overview

:nodoc:

Constant Summary collapse

LESS_THAN =
1
LESS_THAN_OR_EQUAL =
2
GREATER_THAN_OR_EQUAL =
3
GREATER_THAN =
4
@@comparators =
{
	LESS_THAN => '<',
	LESS_THAN_OR_EQUAL => '<=',
	GREATER_THAN_OR_EQUAL => '>=',
	GREATER_THAN => '>'
}
@@mockComparators =
{
	LESS_THAN => Proc.new { |d1, d2| d1 < d2 },
	LESS_THAN_OR_EQUAL => Proc.new { |d1, d2| d1 <= d2 },
	GREATER_THAN_OR_EQUAL => Proc.new { |d1, d2| d1 >= d2 },
	GREATER_THAN => Proc.new { |d1, d2| d1 > d2 }
}

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, compareType) ⇒ Compare

Returns a new instance of Compare.



18
19
20
21
# File 'lib/lafcadio/query/Compare.rb', line 18

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

Instance Method Details

#objectMeets(anObj) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/lafcadio/query/Compare.rb', line 44

def objectMeets(anObj)
	value = anObj.send @fieldName
	value = value.pkId if value.class <= DomainObject
	if value
		@@mockComparators[@compareType].call(value, @searchTerm)
	else
		false
	end
end

#toSqlObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lafcadio/query/Compare.rb', line 23

def toSql
	useFieldForSqlValue = false
	if @fieldName != @objectType.sqlPrimaryKeyName
		field = getField
		useFieldForSqlValue = true unless field.class <= LinkField
	end
	if useFieldForSqlValue
		"#{ dbFieldName } #{ @@comparators[@compareType] } " +
				field.valueForSQL(@searchTerm).to_s
	else
		"#{ dbFieldName } #{ @@comparators[@compareType] } #{ @searchTerm }"
	end
end