m4dbi 0.8.6 → 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/m4dbi/model.rb CHANGED
@@ -416,6 +416,7 @@ module M4DBI
416
416
  def set( hash )
417
417
  set_clause, set_params = hash.to_set_clause
418
418
  set_params << pk
419
+ state_before = self.to_h
419
420
  st = prepare("UPDATE #{table} SET #{set_clause} WHERE #{pk_clause}")
420
421
  num_updated = st.execute( *set_params ).affected_count
421
422
  if num_updated > 0
@@ -425,7 +426,7 @@ module M4DBI
425
426
  if self.class.hooks[:active]
426
427
  self.class.hooks[:after_update].each do |block|
427
428
  self.class.hooks[:active] = false
428
- block.yield self
429
+ block.yield state_before, self
429
430
  self.class.hooks[:active] = true
430
431
  end
431
432
  end
@@ -466,6 +467,15 @@ module M4DBI
466
467
  def save!
467
468
  nil
468
469
  end
470
+
471
+ def to_h
472
+ h = Hash.new
473
+ self.class.columns.each do |col|
474
+ col_name = col['name'].to_s
475
+ h[col_name] = @row[col_name]
476
+ end
477
+ h
478
+ end
469
479
  end
470
480
 
471
481
  # Define a new M4DBI::Model like this:
@@ -546,6 +556,7 @@ module M4DBI
546
556
  # Column writers
547
557
 
548
558
  class_def( "#{method}=".to_sym ) do |new_value|
559
+ state_before = self.to_h
549
560
  stm = prepare("UPDATE #{table} SET #{colname} = ? WHERE #{pk_clause}")
550
561
  num_changed = stm.execute(
551
562
  new_value,
@@ -557,7 +568,7 @@ module M4DBI
557
568
  if self.class.hooks[:active]
558
569
  self.class.hooks[:after_update].each do |block|
559
570
  self.class.hooks[:active] = false
560
- block.yield self
571
+ block.yield state_before, self
561
572
  self.class.hooks[:active] = true
562
573
  end
563
574
  end
@@ -21,7 +21,7 @@ module M4DBI
21
21
  if rows.any?
22
22
  rows[0][0]
23
23
  else
24
- raise RDBI::Error.new( "Query returned no rows. SQL: #{@dbh.last_query}" )
24
+ raise RDBI::Error.new( "Query returned no rows." )
25
25
  end
26
26
  end
27
27
 
data/lib/m4dbi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module M4DBI
2
- VERSION = '0.8.6'
2
+ VERSION = '0.8.7'
3
3
  end
data/spec/model.rb CHANGED
@@ -854,7 +854,7 @@ describe 'A created M4DBI::Model subclass instance' do
854
854
 
855
855
  it 'after setting data, executes code provided in an after_update hook' do
856
856
  class Author < M4DBI::Model( :authors )
857
- after_update do |author|
857
+ after_update do |author_before, author|
858
858
  $test = 3
859
859
  author.name = 'different name'
860
860
  end
@@ -875,9 +875,25 @@ describe 'A created M4DBI::Model subclass instance' do
875
875
  a.name.should.equal 'different name'
876
876
  end
877
877
 
878
+ it 'after setting data, passes a Hash representing the previous state to after_update blocks' do
879
+ class Author < M4DBI::Model( :authors )
880
+ after_update do |author_before, author|
881
+ $test = author_before['name']
882
+ author.name = 'different name'
883
+ end
884
+ end
885
+
886
+ $test.should.not.equal 'theauthor'
887
+ a = Author.create(name: 'theauthor')
888
+ $test.should.not.equal 'theauthor'
889
+ a.name = 'foobar'
890
+ $test.should.equal 'theauthor'
891
+ a.name.should.equal 'different name'
892
+ end
893
+
878
894
  it 'provides a means to remove all after_update hooks' do
879
895
  class Author < M4DBI::Model( :authors )
880
- after_update do |author|
896
+ after_update do |_, _|
881
897
  $test = 'remove after_update'
882
898
  end
883
899
  end
@@ -908,7 +924,7 @@ describe 'A created M4DBI::Model subclass instance' do
908
924
  it 'provides a means to remove all after_delete hooks' do
909
925
  class Author < M4DBI::Model( :authors )
910
926
  after_delete do |author|
911
- $test = 'remove after_update'
927
+ $test = 'remove after_delete'
912
928
  end
913
929
  end
914
930
  class Author < M4DBI::Model( :authors )
@@ -1047,12 +1063,13 @@ describe 'A found M4DBI::Model subclass instance' do
1047
1063
  end
1048
1064
 
1049
1065
  it 'before deletion, executes code provided in an before_delete hook' do
1066
+ $test = 'something'
1050
1067
  class Author < M4DBI::Model( :authors )
1051
1068
  before_delete do |author|
1052
1069
  $test = author.name
1053
1070
  end
1054
1071
  end
1055
- $test.should.not.equal 'theauthor'
1072
+ $test.should.equal 'something'
1056
1073
  a = Author.create(name: 'theauthor')
1057
1074
  $test.should.not.equal 'theauthor'
1058
1075
  a.delete
@@ -1085,6 +1102,19 @@ describe 'A found M4DBI::Model subclass instance' do
1085
1102
  p.save!
1086
1103
  end
1087
1104
  end
1105
+
1106
+ it 'provides a Hash representation' do
1107
+ record = @m_mc.create( id: 12, c1: 50, c2: 44 )
1108
+ record.to_h.should.equal( {
1109
+ 'id' => 12,
1110
+ 'c1' => 50,
1111
+ 'c2' => 44,
1112
+ 'c3' => nil,
1113
+ 'c4' => nil,
1114
+ 'c5' => nil,
1115
+ 'ts' => nil,
1116
+ } )
1117
+ end
1088
1118
  end
1089
1119
 
1090
1120
  describe 'M4DBI::Model (relationships)' do
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.6
4
+ version: 0.8.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-23 00:00:00.000000000 Z
12
+ date: 2013-02-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: metaid
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  requirements:
93
93
  - bacon (optional)
94
94
  rubyforge_project:
95
- rubygems_version: 1.8.18
95
+ rubygems_version: 1.8.25
96
96
  signing_key:
97
97
  specification_version: 3
98
98
  summary: Models (and More) for RDBI