veneer 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -20,6 +20,10 @@ module ActiveRecord
20
20
  klass.find(:all,opts)
21
21
  end
22
22
 
23
+ def before_save(*methods)
24
+ klass.before_save *methods
25
+ end
26
+
23
27
  private
24
28
  def conditional_to_ar_opts(c)
25
29
  opts = {}
@@ -4,16 +4,20 @@ module ActiveRecord
4
4
  class InstanceWrapper < Veneer::Base::InstanceWrapper
5
5
  def update(attributes = {})
6
6
  instance.update_attributes(attributes)
7
+ rescue Veneer::Errors::BeforeSaveError => e
8
+ handle_before_save_error(e)
7
9
  end
8
10
 
9
11
  def save!
10
12
  instance.save!
11
- rescue ActiveRecord::RecordInvalid => e
12
- raise ::Veneer::Errors::NotSaved
13
+ rescue ActiveRecord::RecordInvalid, Veneer::Errors::BeforeSaveError=> e
14
+ raise ::Veneer::Errors::NotSaved, e.message
13
15
  end
14
16
 
15
17
  def save
16
18
  instance.save
19
+ rescue Veneer::Errors::BeforeSaveError => e
20
+ handle_before_save_error(e)
17
21
  end
18
22
  end # InstanceWrapper
19
23
  end # VeneerInterface
@@ -20,6 +20,14 @@ module DataMapper
20
20
  klass.all(opts)
21
21
  end
22
22
 
23
+ def before_save(*methods)
24
+ klass.class_eval do
25
+ methods.each do |meth|
26
+ before :save, meth
27
+ end
28
+ end
29
+ end
30
+
23
31
  private
24
32
  def opts_from_conditional_for_dm(c)
25
33
  opts = {}
@@ -1,23 +1,39 @@
1
- module DataMapper
2
- module Resource
3
- module VeneerInterface
4
- class InstanceWrapper < ::Veneer::Base::InstanceWrapper
1
+ module DataMapper
2
+ module Resource
3
+ module VeneerInterface
4
+ class InstanceWrapper < ::Veneer::Base::InstanceWrapper
5
5
 
6
- def save!
7
- r = instance.save
8
- raise ::Veneer::Errors::NotSaved unless r
9
- r
10
- end
6
+ def save
7
+ result = false
8
+ instance.class.transaction do
9
+ result = instance.save
10
+ end
11
+ result
12
+ rescue ::Veneer::Errors::BeforeSaveError => e
13
+ handle_before_save_error(e)
14
+ end
11
15
 
12
- def new_record?
13
- instance.respond_to?(:new?) ? instance.new? : instance.new_record?
14
- end
16
+ def save!
17
+ result = false
18
+ instance.class.transaction do
19
+ result = instance.save
20
+ raise ::Veneer::Errors::NotSaved unless result
21
+ result
22
+ end
23
+ result
24
+ rescue ::Veneer::Errors::BeforeSaveError => e
25
+ raise ::Veneer::Errors::NotSaved, e.message
26
+ end
27
+
28
+ def new_record?
29
+ instance.respond_to?(:new?) ? instance.new? : instance.new_record?
30
+ end
15
31
 
16
- def destroy
17
- instance.destroy
18
- self
32
+ def destroy
33
+ instance.destroy
34
+ self
35
+ end
19
36
  end
20
37
  end
21
38
  end
22
39
  end
23
- end
@@ -20,6 +20,10 @@ module MongoMapper
20
20
  klass.all(opts)
21
21
  end
22
22
 
23
+ def before_save(*methods)
24
+ klass.before_save *methods
25
+ end
26
+
23
27
  private
24
28
  def conditional_to_mongo_opts(c)
25
29
  opts = {}
@@ -4,8 +4,22 @@ module MongoMapper
4
4
  class InstanceWrapper < ::Veneer::Base::InstanceWrapper
5
5
  def save!
6
6
  instance.save!
7
- rescue
8
- raise ::Veneer::Errors::NotSaved
7
+ rescue ::Veneer::Errors::BeforeSaveError => e
8
+ raise ::Veneer::Errors::NotSaved, e.message
9
+ rescue => e
10
+ raise ::Veneer::Errors::NotSaved, e.message
11
+ end
12
+
13
+ def save
14
+ instance.save
15
+ rescue ::Veneer::Errors::BeforeSaveError => e
16
+ handle_before_save_error(e)
17
+ end
18
+
19
+ def update(*args)
20
+ instance.update(*args)
21
+ rescue ::Veneer::Errors::BeforeSaveError => e
22
+ handle_before_save_error(e)
9
23
  end
10
24
  end
11
25
  end
@@ -6,6 +6,20 @@ module Veneer
6
6
  @instance, @options = instance, opts
7
7
  end
8
8
 
9
+ def class
10
+ @instance.class
11
+ end
12
+
13
+ def handle_before_save_error(e)
14
+ case e.message
15
+ when Array
16
+ instance.errors.add(e.message[0], e.message[1])
17
+ when String
18
+ instance.errors.add("", e.message)
19
+ end
20
+ false
21
+ end
22
+
9
23
  # Checks equality of the instances
10
24
  # @api public
11
25
  def ==(other)
@@ -5,5 +5,6 @@ module Veneer
5
5
  class NotSaved < VeneerError; end
6
6
  class ConditionalOperatorNotSupported < VeneerError; end
7
7
  class NotImplemented < VeneerError; end
8
+ class BeforeSaveError < VeneerError; end
8
9
  end
9
10
  end
@@ -0,0 +1,41 @@
1
+ class Test::Unit::TestCase
2
+ def self.veneer_should_implement_before_save_hooks
3
+ context "impelements before_save" do
4
+ should "setup the test correctly" do
5
+ assert_not_nil @klass
6
+ assert_kind_of Class, @klass
7
+ assert_not_nil @valid_attributes
8
+ assert_kind_of Hash, @valid_attributes
9
+ end
10
+
11
+ context "before_save" do
12
+ setup do
13
+ $captures = []
14
+ Veneer(@klass).before_save :check_before_save
15
+ @klass.class_eval do
16
+ include Veneer::Test::BeforeSaveSetup
17
+ end unless Veneer::Test::BeforeSaveSetup > @klass
18
+ end
19
+
20
+ teardown{ $captures = [] }
21
+
22
+ should "run the before save block" do
23
+ Veneer(@klass).new(@valid_attributes).save
24
+ assert_contains $captures, :check_before_save
25
+ end
26
+
27
+ should "allow me to raise inside the before save without propergating the issue further" do
28
+ assert_nothing_raised do
29
+ Veneer(@klass).new(@valid_attributes.merge(:name => "raise_on_before_save")).save
30
+ end
31
+ end
32
+
33
+ should "allow me to save! with an invalid before_save hook and do the right thing" do
34
+ assert_raise(::Veneer::Errors::NotSaved) do
35
+ Veneer(@klass).new(@valid_attributes.merge(:name => "raise_on_before_save")).save!
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ module Veneer
2
+ module Test
3
+ module BeforeSaveSetup
4
+ def check_before_save
5
+ $captures << :check_before_save
6
+ if name == "raise_on_before_save"
7
+ raise Veneer::Errors::BeforeSaveError, [:name, "cannot be raise_on_before_save"]
8
+ else
9
+ true
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -15,7 +15,8 @@ module Veneer
15
15
  veneer_should_impelement_destroy_all
16
16
  veneer_should_implement_create_with_invalid_attributes
17
17
  veneer_should_implement_find
18
- end
18
+ veneer_should_implement_before_save_hooks
19
+ end
19
20
  end
20
21
  end
21
22
  end
@@ -16,6 +16,7 @@ module Veneer
16
16
  veneer_should_impelement_destroy_all
17
17
  veneer_should_implement_create_with_invalid_attributes
18
18
  veneer_should_implement_find
19
+ veneer_should_implement_before_save_hooks
19
20
  end
20
21
  end
21
22
  end
@@ -16,6 +16,7 @@ module Veneer
16
16
  veneer_should_impelement_destroy_all
17
17
  veneer_should_implement_create_with_invalid_attributes
18
18
  veneer_should_implement_find
19
+ veneer_should_implement_before_save_hooks
19
20
  end
20
21
  end
21
22
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{veneer}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Daniel Neighman"]
12
- s.date = %q{2009-12-05}
12
+ s.date = %q{2009-12-06}
13
13
  s.description = %q{Veneer provides basic querying, saving, deleteing and creating of data stores.}
14
14
  s.email = %q{has.sox@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -45,10 +45,12 @@ Gem::Specification.new do |s|
45
45
  "test/macros/class_wrapper/create_macro.rb",
46
46
  "test/macros/class_wrapper/delete_macro.rb",
47
47
  "test/macros/class_wrapper/find_macro.rb",
48
+ "test/macros/class_wrapper/hooks.rb",
48
49
  "test/macros/instance_wrapper/destroy_macro.rb",
49
50
  "test/macros/instance_wrapper/new_record_macro.rb",
50
51
  "test/macros/instance_wrapper/save_macro.rb",
51
52
  "test/macros/veneer_constants_interface.rb",
53
+ "test/support/before_save_mixin.rb",
52
54
  "test/support/helpers.rb",
53
55
  "test/test_helper.rb",
54
56
  "test/veneer/adapters/active_record/active_record_setup.rb",
@@ -74,10 +76,12 @@ Gem::Specification.new do |s|
74
76
  "test/macros/class_wrapper/create_macro.rb",
75
77
  "test/macros/class_wrapper/delete_macro.rb",
76
78
  "test/macros/class_wrapper/find_macro.rb",
79
+ "test/macros/class_wrapper/hooks.rb",
77
80
  "test/macros/instance_wrapper/destroy_macro.rb",
78
81
  "test/macros/instance_wrapper/new_record_macro.rb",
79
82
  "test/macros/instance_wrapper/save_macro.rb",
80
83
  "test/macros/veneer_constants_interface.rb",
84
+ "test/support/before_save_mixin.rb",
81
85
  "test/support/helpers.rb",
82
86
  "test/test_helper.rb",
83
87
  "test/veneer/adapters/active_record/active_record_setup.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: veneer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Neighman
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-05 00:00:00 +11:00
12
+ date: 2009-12-06 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -51,10 +51,12 @@ files:
51
51
  - test/macros/class_wrapper/create_macro.rb
52
52
  - test/macros/class_wrapper/delete_macro.rb
53
53
  - test/macros/class_wrapper/find_macro.rb
54
+ - test/macros/class_wrapper/hooks.rb
54
55
  - test/macros/instance_wrapper/destroy_macro.rb
55
56
  - test/macros/instance_wrapper/new_record_macro.rb
56
57
  - test/macros/instance_wrapper/save_macro.rb
57
58
  - test/macros/veneer_constants_interface.rb
59
+ - test/support/before_save_mixin.rb
58
60
  - test/support/helpers.rb
59
61
  - test/test_helper.rb
60
62
  - test/veneer/adapters/active_record/active_record_setup.rb
@@ -101,10 +103,12 @@ test_files:
101
103
  - test/macros/class_wrapper/create_macro.rb
102
104
  - test/macros/class_wrapper/delete_macro.rb
103
105
  - test/macros/class_wrapper/find_macro.rb
106
+ - test/macros/class_wrapper/hooks.rb
104
107
  - test/macros/instance_wrapper/destroy_macro.rb
105
108
  - test/macros/instance_wrapper/new_record_macro.rb
106
109
  - test/macros/instance_wrapper/save_macro.rb
107
110
  - test/macros/veneer_constants_interface.rb
111
+ - test/support/before_save_mixin.rb
108
112
  - test/support/helpers.rb
109
113
  - test/test_helper.rb
110
114
  - test/veneer/adapters/active_record/active_record_setup.rb