m4dbi 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/m4dbi/model.rb CHANGED
@@ -2,7 +2,7 @@ module M4DBI
2
2
  class Model
3
3
  #attr_reader :row
4
4
  ancestral_trait_reader :dbh, :table
5
- ancestral_trait_class_reader :dbh, :table, :pk, :columns, :st, :callbacks
5
+ ancestral_trait_class_reader :dbh, :table, :pk, :columns, :st, :hooks
6
6
 
7
7
  M4DBI_UNASSIGNED = '__m4dbi_unassigned__'
8
8
 
@@ -142,21 +142,21 @@ module M4DBI
142
142
  end
143
143
  if ! pk_hash.empty?
144
144
  rec = self.one_where( pk_hash )
145
- if callbacks[:active]
146
- callbacks[:after_create].each do |block|
147
- callbacks[:active] = false
145
+ if hooks[:active]
146
+ hooks[:after_create].each do |block|
147
+ hooks[:active] = false
148
148
  block.yield rec
149
- callbacks[:active] = true
149
+ hooks[:active] = true
150
150
  end
151
151
  end
152
152
  else
153
153
  begin
154
154
  rec = last_record( dbh_ )
155
- if callbacks[:active]
156
- callbacks[:after_create].each do |block|
157
- callbacks[:active] = false
155
+ if hooks[:active]
156
+ hooks[:after_create].each do |block|
157
+ hooks[:active] = false
158
158
  block.yield rec
159
- callbacks[:active] = true
159
+ hooks[:active] = true
160
160
  end
161
161
  end
162
162
  rescue NoMethodError => e
@@ -236,15 +236,35 @@ module M4DBI
236
236
  end
237
237
 
238
238
  def self.after_create(&block)
239
- callbacks[:after_create] << block
239
+ hooks[:after_create] << block
240
+ end
241
+
242
+ def self.remove_after_create_hooks
243
+ hooks[:after_create].clear
240
244
  end
241
245
 
242
246
  def self.after_update(&block)
243
- callbacks[:after_update] << block
247
+ hooks[:after_update] << block
248
+ end
249
+
250
+ def self.remove_after_update_hooks
251
+ hooks[:after_update].clear
252
+ end
253
+
254
+ def self.before_delete(&block)
255
+ hooks[:before_delete] << block
244
256
  end
245
257
 
246
258
  def self.after_delete(&block)
247
- callbacks[:after_delete] << block
259
+ hooks[:after_delete] << block
260
+ end
261
+
262
+ def self.remove_before_delete_hooks
263
+ hooks[:before_delete].clear
264
+ end
265
+
266
+ def self.remove_after_delete_hooks
267
+ hooks[:after_delete].clear
248
268
  end
249
269
 
250
270
  # Example:
@@ -395,11 +415,11 @@ module M4DBI
395
415
  hash.each do |key,value|
396
416
  @row[ key ] = value
397
417
  end
398
- if self.class.callbacks[:active]
399
- self.class.callbacks[:after_update].each do |block|
400
- self.class.callbacks[:active] = false
418
+ if self.class.hooks[:active]
419
+ self.class.hooks[:after_update].each do |block|
420
+ self.class.hooks[:active] = false
401
421
  block.yield self
402
- self.class.callbacks[:active] = true
422
+ self.class.hooks[:active] = true
403
423
  end
404
424
  end
405
425
  end
@@ -408,16 +428,24 @@ module M4DBI
408
428
 
409
429
  # Returns true iff the record and only the record was successfully deleted.
410
430
  def delete
431
+ if self.class.hooks[:active]
432
+ self.class.hooks[:before_delete].each do |block|
433
+ self.class.hooks[:active] = false
434
+ block.yield self
435
+ self.class.hooks[:active] = true
436
+ end
437
+ end
438
+
411
439
  st = prepare("DELETE FROM #{table} WHERE #{pk_clause}")
412
440
  num_deleted = st.execute( *pk_values ).affected_count
413
441
  if num_deleted != 1
414
442
  false
415
443
  else
416
- if self.class.callbacks[:active]
417
- self.class.callbacks[:after_delete].each do |block|
418
- self.class.callbacks[:active] = false
444
+ if self.class.hooks[:active]
445
+ self.class.hooks[:after_delete].each do |block|
446
+ self.class.hooks[:active] = false
419
447
  block.yield self
420
- self.class.callbacks[:active] = true
448
+ self.class.hooks[:active] = true
421
449
  end
422
450
  end
423
451
  true
@@ -462,9 +490,10 @@ module M4DBI
462
490
  :pk => pk_,
463
491
  :columns => h.table_schema( table.to_sym ).columns,
464
492
  :st => Hash.new, # prepared statements for all queries
465
- :callbacks => {
493
+ :hooks => {
466
494
  after_create: [],
467
495
  after_update: [],
496
+ before_delete: [],
468
497
  after_delete: [],
469
498
  active: true,
470
499
  },
@@ -518,11 +547,11 @@ module M4DBI
518
547
  if num_changed > 0
519
548
  @row[ colname ] = new_value
520
549
  end
521
- if self.class.callbacks[:active]
522
- self.class.callbacks[:after_update].each do |block|
523
- self.class.callbacks[:active] = false
550
+ if self.class.hooks[:active]
551
+ self.class.hooks[:after_update].each do |block|
552
+ self.class.hooks[:active] = false
524
553
  block.yield self
525
- self.class.callbacks[:active] = true
554
+ self.class.hooks[:active] = true
526
555
  end
527
556
  end
528
557
  new_value
data/lib/m4dbi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module M4DBI
2
- VERSION = '0.8.3'
2
+ VERSION = '0.8.4'
3
3
  end
data/spec/model.rb CHANGED
@@ -41,7 +41,11 @@ describe 'A M4DBI::Model subclass' do
41
41
  @m_mc = Class.new( M4DBI::Model( :many_col_table ) )
42
42
  @m_nipk = Class.new( M4DBI::Model( :non_id_pk, :pk => [ :str ] ) )
43
43
  @m_mcpk = Class.new( M4DBI::Model( :mcpk, :pk => [ :kc1, :kc2 ] ) )
44
- class Author < M4DBI::Model( :authors ); end
44
+ class Author < M4DBI::Model( :authors )
45
+ remove_after_create_hooks
46
+ remove_after_update_hooks
47
+ remove_after_delete_hooks
48
+ end
45
49
  end
46
50
 
47
51
  it 'can be defined' do
@@ -85,7 +89,7 @@ describe 'A M4DBI::Model subclass' do
85
89
  end
86
90
  end
87
91
 
88
- it 'provides the database handle it is using' do
92
+ # it 'provides the database handle it is using' do
89
93
  # begin
90
94
  # @m_author.dbh.should.equal $dbh
91
95
 
@@ -96,7 +100,7 @@ describe 'A M4DBI::Model subclass' do
96
100
  # # Clean up handles for later specs
97
101
  # connect_to_spec_database
98
102
  # end
99
- end
103
+ # end
100
104
 
101
105
  it 'maintains distinction from models of the same name in different databases' do
102
106
  begin
@@ -683,6 +687,20 @@ describe 'A M4DBI::Model subclass' do
683
687
  $test.should.equal 2
684
688
  a.name.should.equal 'different name'
685
689
  end
690
+
691
+ it 'provides a means to remove all after_create hooks' do
692
+ class Author < M4DBI::Model( :authors )
693
+ after_create do |author|
694
+ $test = 'remove after_create'
695
+ end
696
+ end
697
+ class Author < M4DBI::Model( :authors )
698
+ remove_after_create_hooks
699
+ end
700
+ $test.should.not.equal 'remove after_create'
701
+ a = Author.create(name: 'theauthor')
702
+ $test.should.not.equal 'remove after_create'
703
+ end
686
704
  end
687
705
 
688
706
  describe 'A created M4DBI::Model subclass instance' do
@@ -846,6 +864,51 @@ describe 'A created M4DBI::Model subclass instance' do
846
864
  $test.should.equal 3
847
865
  a.name.should.equal 'different name'
848
866
  end
867
+
868
+ it 'provides a means to remove all after_update hooks' do
869
+ class Author < M4DBI::Model( :authors )
870
+ after_update do |author|
871
+ $test = 'remove after_update'
872
+ end
873
+ end
874
+ class Author < M4DBI::Model( :authors )
875
+ remove_after_update_hooks
876
+ end
877
+ $test.should.not.equal 'remove after_update'
878
+ a = Author.create(name: 'theauthor')
879
+ a.name = 'another author'
880
+ $test.should.not.equal 'remove after_update'
881
+ end
882
+
883
+ it 'provides a means to remove all before_delete hooks' do
884
+ class Author < M4DBI::Model( :authors )
885
+ before_delete do |author|
886
+ $test = 'remove before_update'
887
+ end
888
+ end
889
+ class Author < M4DBI::Model( :authors )
890
+ remove_before_delete_hooks
891
+ end
892
+ $test.should.not.equal 'remove before_delete'
893
+ a = Author.create(name: 'theauthor')
894
+ a.delete
895
+ $test.should.not.equal 'remove before_delete'
896
+ end
897
+
898
+ it 'provides a means to remove all after_delete hooks' do
899
+ class Author < M4DBI::Model( :authors )
900
+ after_delete do |author|
901
+ $test = 'remove after_update'
902
+ end
903
+ end
904
+ class Author < M4DBI::Model( :authors )
905
+ remove_after_delete_hooks
906
+ end
907
+ $test.should.not.equal 'remove after_delete'
908
+ a = Author.create(name: 'theauthor')
909
+ a.delete
910
+ $test.should.not.equal 'remove after_delete'
911
+ end
849
912
  end
850
913
 
851
914
  describe 'A found M4DBI::Model subclass instance' do
@@ -973,6 +1036,19 @@ describe 'A found M4DBI::Model subclass instance' do
973
1036
  reset_data
974
1037
  end
975
1038
 
1039
+ it 'before deletion, executes code provided in an before_delete hook' do
1040
+ class Author < M4DBI::Model( :authors )
1041
+ before_delete do |author|
1042
+ $test = author.name
1043
+ end
1044
+ end
1045
+ $test.should.not.equal 'theauthor'
1046
+ a = Author.create(name: 'theauthor')
1047
+ $test.should.not.equal 'theauthor'
1048
+ a.delete
1049
+ $test.should.equal 'theauthor'
1050
+ end
1051
+
976
1052
  it 'after deletion, executes code provided in an after_delete hook' do
977
1053
  class Author < M4DBI::Model( :authors )
978
1054
  after_delete do |author|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: m4dbi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: