Class: Lafcadio::ObjectStore::MockDbBridge

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

Overview

:nodoc:

Defined Under Namespace

Classes: Transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMockDbBridge

Returns a new instance of MockDbBridge.



10
11
12
13
14
15
# File 'lib/lafcadio/mock.rb', line 10

def initialize
	@objects = {}
	@next_pk_ids = {}
	@queries = []
	@transaction = nil
end

Instance Attribute Details

#last_pk_id_insertedObject (readonly)

Returns the value of attribute last_pk_id_inserted.



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

def last_pk_id_inserted
  @last_pk_id_inserted
end

#next_pk_idsObject

Returns the value of attribute next_pk_ids.



8
9
10
# File 'lib/lafcadio/mock.rb', line 8

def next_pk_ids
  @next_pk_ids
end

#objectsObject

Returns the value of attribute objects.



8
9
10
# File 'lib/lafcadio/mock.rb', line 8

def objects
  @objects
end

#queries(domain_class = nil) ⇒ Object

Returns the value of attribute queries.



8
9
10
# File 'lib/lafcadio/mock.rb', line 8

def queries
  @queries
end

Instance Method Details

#all(domain_class) ⇒ Object



17
18
19
# File 'lib/lafcadio/mock.rb', line 17

def all( domain_class )
	@objects[domain_class] ? @objects[domain_class].values : []
end

#commit(db_object) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lafcadio/mock.rb', line 21

def commit(db_object)
	if db_object.pk_id and !db_object.pk_id.is_a?( Integer )
		raise ArgumentError
	end
	if @transaction
		@transaction << db_object
	else
		objects_by_domain_class = objects_by_domain_class(
			db_object.domain_class
		)
		if db_object.delete
			objects_by_domain_class.delete( db_object.pk_id )
		else
			object_pk_id = pre_commit_pk_id( db_object )
			objects_by_domain_class[object_pk_id] = db_object
		end
	end
end

#group_query(query) ⇒ Object



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

def group_query( query )
	query.collect objects_by_domain_class( query.domain_class ).values
end

#next_pk_id(domain_class) ⇒ Object



44
45
46
47
48
49
# File 'lib/lafcadio/mock.rb', line 44

def next_pk_id( domain_class )
	dobjs = objects_by_domain_class( domain_class ).values
	dobjs.inject( 0 ) { |memo, obj|
		memo > obj.pk_id ? memo : obj.pk_id
	} + 1
end

#objects_by_domain_class(domain_class) ⇒ Object



51
52
53
54
# File 'lib/lafcadio/mock.rb', line 51

def objects_by_domain_class( domain_class )
	@objects[domain_class] = {} unless @objects[domain_class]
	@objects[domain_class]
end

#pre_commit_pk_id(domain_object) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lafcadio/mock.rb', line 56

def pre_commit_pk_id( domain_object )
	@next_pk_ids = {} unless @next_pk_ids
	if (next_pk_id = @next_pk_ids[domain_object.domain_class])
		@next_pk_ids[domain_object.domain_class] = nil
		@last_pk_id_inserted = next_pk_id
	elsif domain_object.pk_id
		domain_object.pk_id
	else
		pk_ids = objects_by_domain_class( domain_object.domain_class ).keys
		@last_pk_id_inserted = pk_ids.max ? pk_ids.max + 1 : 1
	end
end

#query_count(sql) ⇒ Object



75
76
77
# File 'lib/lafcadio/mock.rb', line 75

def query_count( sql )
	@queries.select { |qry| qry.to_sql == sql }.size
end

#rollbackObject



79
# File 'lib/lafcadio/mock.rb', line 79

def rollback; end

#select_dobjs(query) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lafcadio/mock.rb', line 81

def select_dobjs( query )
	@queries << query
	domain_class = query.domain_class
	all = all domain_class
	if @transaction
		from_tr = @transaction.select { |dobj|
			dobj.domain_class == query.domain_class
		}
		all = all.concat from_tr
	end
	objects = all.select { |dobj| query.dobj_satisfies?( dobj ) }
	query.order_and_limit_collection objects
end

#set_next_pk_id(domain_class, npi) ⇒ Object



95
96
97
98
# File 'lib/lafcadio/mock.rb', line 95

def set_next_pk_id( domain_class, npi )
	@next_pk_ids = {} unless @next_pk_ids
	@next_pk_ids[ domain_class ] = npi
end

#transaction(action) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/lafcadio/mock.rb', line 100

def transaction( action )
	tr = MockDbBridge::Transaction.new
	@transaction = tr
	action.call tr
	@transaction = nil
	tr.uniq!
	tr.each do |dobj_to_commit| commit( dobj_to_commit ); end
end

#transactional_cloneObject



109
110
111
112
113
114
115
# File 'lib/lafcadio/mock.rb', line 109

def transactional_clone
	tc = clone
	tc.objects = objects.clone
	tc.next_pk_ids = next_pk_ids.clone
	tc.queries = queries.clone
	tc
end