mongomatic 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -89,21 +89,34 @@ module Mongomatic
89
89
 
90
90
  def insert(opts={})
91
91
  return false unless new? && valid?
92
+ self.send(:before_insert) if self.respond_to?(:before_insert)
93
+ self.send(:before_insert_or_update) if self.respond_to?(:before_insert_or_update)
92
94
  if ret = self.class.collection.insert(@doc,opts)
93
95
  @doc["_id"] = @doc.delete(:_id); ret
94
96
  end
97
+ self.send(:after_insert) if self.respond_to?(:after_insert)
98
+ self.send(:after_insert_or_update) if self.respond_to?(:after_insert_or_update)
99
+ ret
95
100
  end
96
101
 
97
102
  def update(opts={},update_doc=@doc)
98
103
  return false if new? || removed? || !valid?
99
- self.class.collection.update({"_id" => @doc["_id"]}, update_doc, opts)
104
+ self.send(:before_update) if self.respond_to?(:before_update)
105
+ self.send(:before_insert_or_update) if self.respond_to?(:before_insert_or_update)
106
+ ret = self.class.collection.update({"_id" => @doc["_id"]}, update_doc, opts)
107
+ self.send(:after_update) if self.respond_to?(:after_update)
108
+ self.send(:after_insert_or_update) if self.respond_to?(:after_insert_or_update)
109
+ ret
100
110
  end
101
111
 
102
112
  def remove(opts={})
103
113
  return false if new?
114
+ self.send(:before_remove) if self.respond_to?(:before_remove)
104
115
  if ret = self.class.collection.remove({"_id" => @doc["_id"]})
105
116
  self.removed = true; freeze; ret
106
117
  end
118
+ self.send(:after_remove) if self.respond_to?(:after_remove)
119
+ ret
107
120
  end
108
121
 
109
122
  def to_hash
@@ -9,7 +9,10 @@ module Mongomatic
9
9
  #
10
10
  # Returns true if no errors were added otherwise false. Only executes validations that have no :groups option specified
11
11
  def valid?
12
- valid_for_group?(nil)
12
+ self.send(:before_validate) if self.respond_to?(:before_validate)
13
+ r = valid_for_group?(nil)
14
+ self.send(:after_validate) if self.respond_to?(:after_validate)
15
+ r
13
16
  end
14
17
 
15
18
  # call-seq: errors
data/test/helper.rb CHANGED
@@ -11,6 +11,58 @@ Mongomatic.settings = { :connection => ["localhost", 27017, {}], :db => "mongoma
11
11
 
12
12
  class Person < Mongomatic::Base
13
13
  validates_presence_of :name
14
+ attr_accessor :callback_tests
15
+
16
+ def before_validate
17
+ self.callback_tests ||= []
18
+ self.callback_tests << :before_validate
19
+ end
20
+
21
+ def after_validate
22
+ self.callback_tests ||= []
23
+ self.callback_tests << :after_validate
24
+ end
25
+
26
+ def before_insert
27
+ self.callback_tests ||= []
28
+ self.callback_tests << :before_insert
29
+ end
30
+
31
+ def before_insert_or_update
32
+ self.callback_tests ||= []
33
+ self.callback_tests << :before_insert_or_update
34
+ end
35
+
36
+ def after_insert_or_update
37
+ self.callback_tests ||= []
38
+ self.callback_tests << :after_insert_or_update
39
+ end
40
+
41
+ def after_insert
42
+ self.callback_tests ||= []
43
+ self.callback_tests << :after_insert
44
+ end
45
+
46
+ def before_update
47
+ self.callback_tests ||= []
48
+ self.callback_tests << :before_update
49
+ end
50
+
51
+ def after_update
52
+ self.callback_tests ||= []
53
+ self.callback_tests << :after_update
54
+ end
55
+
56
+ def before_remove
57
+ self.callback_tests ||= []
58
+ self.callback_tests << :before_remove
59
+ end
60
+
61
+ def after_remove
62
+ self.callback_tests ||= []
63
+ self.callback_tests << :after_remove
64
+ end
65
+
14
66
  end
15
67
 
16
68
  class Test::Unit::TestCase
@@ -1,6 +1,17 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestMongomatic < Test::Unit::TestCase
4
+ should "work with enumerable methods" do
5
+ Person.collection.remove
6
+ p1 = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
7
+ p2 = Person.new(:name => "Ben2", :birth_year => 1986, :created_at => Time.now.utc, :admin => true)
8
+ assert p1.insert.is_a?(BSON::ObjectID)
9
+ assert p2.insert.is_a?(BSON::ObjectID)
10
+ assert_equal 2, Person.collection.count
11
+ assert_equal 2, Person.find.inject(0) { |sum, p| assert p.is_a?(Person); sum += 1 }
12
+ assert_equal p2, Person.find.max { |p1,p2| p1["birth_year"] <=> p2["birth_year"] }
13
+ end
14
+
4
15
  should "be able to insert, update, remove documents" do
5
16
  Person.collection.remove
6
17
 
@@ -98,4 +109,21 @@ class TestMongomatic < Test::Unit::TestCase
98
109
  p = Person.find({"_id" => p["_id"]}).next
99
110
  assert_equal 1986, p["birth_year"]
100
111
  end
112
+
113
+ should "have callbacks" do
114
+ p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
115
+ p.callback_tests = []
116
+ assert p.callback_tests.empty?
117
+ assert p.valid?
118
+ assert_equal [:before_validate, :after_validate], p.callback_tests
119
+ p.callback_tests = []
120
+ assert p.insert.is_a?(BSON::ObjectID)
121
+ assert_equal [:before_validate, :after_validate, :before_insert, :before_insert_or_update, :after_insert, :after_insert_or_update], p.callback_tests
122
+ p.callback_tests = []
123
+ p.update
124
+ assert_equal [:before_validate, :after_validate, :before_update, :before_insert_or_update, :after_update, :after_insert_or_update], p.callback_tests
125
+ p.callback_tests = []
126
+ p.remove
127
+ assert_equal [:before_remove, :after_remove], p.callback_tests
128
+ end
101
129
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 7
9
- version: 0.0.7
8
+ - 8
9
+ version: 0.0.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles
@@ -14,13 +14,14 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-29 00:00:00 -07:00
17
+ date: 2010-08-06 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: shoulda
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ">="
26
27
  - !ruby/object:Gem::Version
@@ -35,6 +36,7 @@ dependencies:
35
36
  name: bson
36
37
  prerelease: false
37
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
38
40
  requirements:
39
41
  - - "="
40
42
  - !ruby/object:Gem::Version
@@ -49,6 +51,7 @@ dependencies:
49
51
  name: bson_ext
50
52
  prerelease: false
51
53
  requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
52
55
  requirements:
53
56
  - - "="
54
57
  - !ruby/object:Gem::Version
@@ -63,6 +66,7 @@ dependencies:
63
66
  name: mongo
64
67
  prerelease: false
65
68
  requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
66
70
  requirements:
67
71
  - - "="
68
72
  - !ruby/object:Gem::Version
@@ -77,6 +81,7 @@ dependencies:
77
81
  name: activesupport
78
82
  prerelease: false
79
83
  requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
80
85
  requirements:
81
86
  - - ">="
82
87
  - !ruby/object:Gem::Version
@@ -125,6 +130,8 @@ files:
125
130
  - lib/mongomatic/validatable/validations/validation_base.rb
126
131
  - LICENSE
127
132
  - README.rdoc
133
+ - test/helper.rb
134
+ - test/test_mongomatic.rb
128
135
  has_rdoc: true
129
136
  homepage: http://github.com/benmyles/mongomatic
130
137
  licenses: []
@@ -135,6 +142,7 @@ rdoc_options:
135
142
  require_paths:
136
143
  - lib
137
144
  required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
138
146
  requirements:
139
147
  - - ">="
140
148
  - !ruby/object:Gem::Version
@@ -142,6 +150,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
150
  - 0
143
151
  version: "0"
144
152
  required_rubygems_version: !ruby/object:Gem::Requirement
153
+ none: false
145
154
  requirements:
146
155
  - - ">="
147
156
  - !ruby/object:Gem::Version
@@ -151,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
160
  requirements: []
152
161
 
153
162
  rubyforge_project:
154
- rubygems_version: 1.3.6
163
+ rubygems_version: 1.3.7
155
164
  signing_key:
156
165
  specification_version: 3
157
166
  summary: Mongomatic is a simple Ruby object mapper for Mongo