Class: Lafcadio::MockDbBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/lafcadio/mock/MockDbBridge.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMockDbBridge

Returns a new instance of MockDbBridge.



5
6
7
8
9
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 5

def initialize
	@objects = {}
	@retrievalsByType = Hash.new 0
	@query_count = Hash.new( 0 )
end

Instance Attribute Details

#lastPkIdInsertedObject (readonly)

Returns the value of attribute lastPkIdInserted.



3
4
5
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 3

def lastPkIdInserted
  @lastPkIdInserted
end

#query_countObject (readonly)

Returns the value of attribute query_count.



3
4
5
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 3

def query_count
  @query_count
end

#retrievalsByTypeObject (readonly)

Returns the value of attribute retrievalsByType.



3
4
5
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 3

def retrievalsByType
  @retrievalsByType
end

Instance Method Details

#_getAll(objectType) ⇒ Object



39
40
41
42
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 39

def _getAll(objectType)
	@retrievalsByType[objectType] = @retrievalsByType[objectType] + 1
	@objects[objectType] ? @objects[objectType].values : []
end

#addObject(dbObject) ⇒ Object



11
12
13
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 11

def addObject(dbObject)
	commit dbObject
end

#collection(objectType, objects) ⇒ Object



37
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 37

def collection(objectType, objects); objects; end

#commit(dbObject) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 15

def commit(dbObject)
	objectsByObjectType = @objects[dbObject.objectType]
	unless objectsByObjectType
		objectsByObjectType = {}
		@objects[dbObject.objectType] = objectsByObjectType
	end
	if dbObject.delete
		objectsByObjectType.delete dbObject.pkId
	else
		object_pkId = dbObject.pkId
		unless object_pkId
			maxpkId = 0
			objectsByObjectType.keys.each { |pkId|
				maxpkId = pkId if pkId > maxpkId
			}
			@lastPkIdInserted = maxpkId + 1
			object_pkId = @lastPkIdInserted
		end
		objectsByObjectType[object_pkId] = dbObject
	end
end

#getCollectionByQuery(query) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 44

def getCollectionByQuery(query)
	@query_count[query] += 1
	objectType = query.objectType
	condition = query.condition
	objects = []
	_getAll( objectType ).each { |dbObj|
		if condition
			objects << dbObj if condition.objectMeets(dbObj)
		else
			objects << dbObj
		end
	}
	coll = collection( objectType, objects )
	if (range = query.limit)
		coll = coll[0..(range.last - range.first)]
	end
	if ( order_by = query.orderBy )
		coll = coll.sort_by { |dobj| dobj.send( order_by ) }
		coll.reverse! if query.orderByOrder == Query::DESC
	end
	coll
end

#group_query(query) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/lafcadio/mock/MockDbBridge.rb', line 67

def group_query( query )
	if query.class == Query::Max
		if ( query.field_name == query.objectType.sqlPrimaryKeyName ||
		     query.field_name == 'rate' )
			query.collect( @objects[query.objectType].values )
		else
			raise "Can't handle query with sql '#{ query.toSql }'"
		end
	end
end