Class: Lafcadio::MockDbBridge

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

Overview

:nodoc:

Defined Under Namespace

Classes: RollbackError, Transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMockDbBridge

Returns a new instance of MockDbBridge.



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

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.



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

def last_pk_id_inserted
  @last_pk_id_inserted
end

Instance Method Details

#all(domain_class) ⇒ Object



15
16
17
# File 'lib/lafcadio/mock.rb', line 15

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

#commit(db_object) ⇒ Object



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

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



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

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

#next_pk_id(domain_class) ⇒ Object



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

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



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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lafcadio/mock.rb', line 50

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
	elsif ( next_pk_id = @next_pk_ids[domain_object.domain_class] )
		@last_pk_id_inserted = next_pk_id
		@next_pk_ids[domain_object.domain_class] = nil
		next_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

#queries(domain_class = nil) ⇒ Object



67
68
69
70
71
# File 'lib/lafcadio/mock.rb', line 67

def queries( domain_class = nil )
	@queries.select { |qry|
		domain_class ? qry.domain_class == domain_class : true
	}
end

#query_count(sql) ⇒ Object



73
74
75
# File 'lib/lafcadio/mock.rb', line 73

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

#select_dobjs(query) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/lafcadio/mock.rb', line 77

def select_dobjs(query)
	@queries << query
	domain_class = query.domain_class
	objects = []
	all( domain_class ).each { |dbObj|
		objects << dbObj if query.dobj_satisfies?( dbObj )
	}
	query.order_and_limit_collection objects
end

#set_next_pk_id(domain_class, npi) ⇒ Object



87
88
89
90
# File 'lib/lafcadio/mock.rb', line 87

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



92
93
94
95
96
97
98
99
100
# File 'lib/lafcadio/mock.rb', line 92

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