jnunemaker-mongomapper 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History CHANGED
@@ -1,3 +1,8 @@
1
+ 0.1.2 7/3/2009
2
+ * 2 minor changes
3
+ * Straightened out callbacks and added validate, validate_on_create and validate_on_update.
4
+ * Attributes passed into attributes= now call writer methods if they exist. This is mostly for virtual attributes.
5
+
1
6
  0.1.1 6/28/2009
2
7
  * 1 minor change
3
8
  * bumped ruby driver to 0.9 and removed hacks I had in while waiting for it
data/README.rdoc CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  Awesome gem for modeling your domain and storing it in mongo.
4
4
 
5
+ == Note on Releases
6
+
7
+ Releases are tagged on github and also released as gems on github and rubyforge. Master is pushed to whenever I add a patch or a new feature. To build from master, you can clone the code and run 'rake install' to install the gem.
8
+
9
+ == Note on Patches/Pull Requests
10
+
11
+ * Fork the project.
12
+ * Make your feature addition or bug fix.
13
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
5
16
  == Dependencies
6
17
 
7
18
  * ActiveSupport (activesupport)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -11,6 +11,11 @@ module MongoMapper
11
11
  key :_id, String
12
12
  key :created_at, Time
13
13
  key :updated_at, Time
14
+
15
+ define_callbacks :before_create, :after_create,
16
+ :before_update, :after_update,
17
+ :before_save, :after_save,
18
+ :before_destroy, :after_destroy
14
19
  end
15
20
  end
16
21
 
@@ -9,13 +9,6 @@ module MongoMapper
9
9
  include Validatable
10
10
  include ActiveSupport::Callbacks
11
11
  include MongoMapper::Serialization
12
-
13
- define_callbacks :before_validation_on_create, :before_validation_on_update,
14
- :before_validation, :after_validation,
15
- :before_create, :after_create,
16
- :before_update, :after_update,
17
- :before_save, :after_save,
18
- :before_destroy, :after_destroy
19
12
  end
20
13
  end
21
14
 
@@ -131,7 +124,12 @@ module MongoMapper
131
124
 
132
125
  def attributes=(attrs)
133
126
  attrs.each_pair do |key_name, value|
134
- write_attribute(key_name, value) if writer?(key_name)
127
+ if writer?(key_name)
128
+ write_attribute(key_name, value)
129
+ else
130
+ writer_method ="#{key_name}="
131
+ self.send(writer_method, value) if respond_to?(writer_method)
132
+ end
135
133
  end
136
134
  end
137
135
 
@@ -4,6 +4,10 @@ module MongoMapper
4
4
  base.class_eval do
5
5
  alias_method_chain :valid?, :callbacks
6
6
  alias_method_chain :save, :validation
7
+
8
+ define_callbacks :before_validation_on_create, :before_validation_on_update,
9
+ :before_validation, :after_validation,
10
+ :validate, :validate_on_create, :validate_on_update
7
11
  end
8
12
  end
9
13
 
@@ -12,16 +16,34 @@ module MongoMapper
12
16
  end
13
17
 
14
18
  private
15
- def save_with_validation
16
- new? ? run_callbacks(:before_validation_on_create) :
17
- run_callbacks(:before_validation_on_update)
18
-
19
- valid? ? save_without_validation : false
19
+ def save_with_validation
20
+ if valid?
21
+ save_without_validation
22
+ else
23
+ false
24
+ end
20
25
  end
21
26
 
22
27
  def valid_with_callbacks?
23
28
  run_callbacks(:before_validation)
24
- run_callbacks(:after_validation) if valid_without_callbacks?
29
+
30
+ if new?
31
+ run_callbacks(:before_validation_on_create)
32
+ else
33
+ run_callbacks(:before_validation_on_update)
34
+ end
35
+
36
+ run_callbacks(:validate)
37
+
38
+ if new?
39
+ run_callbacks(:validate_on_create)
40
+ else
41
+ run_callbacks(:validate_on_update)
42
+ end
43
+
44
+ is_valid = valid_without_callbacks?
45
+ run_callbacks(:after_validation) if is_valid
46
+ is_valid
25
47
  end
26
48
  end
27
49
  end
data/mongomapper.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{mongomapper}
5
- s.version = "0.1.1"
5
+ s.version = "0.1.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["John Nunemaker"]
9
- s.date = %q{2009-06-28}
9
+ s.date = %q{2009-07-03}
10
10
  s.email = %q{nunemaker@gmail.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
@@ -15,7 +15,8 @@ class CallbacksTest < Test::Unit::TestCase
15
15
  :before_create, :after_create,
16
16
  :before_update, :after_update,
17
17
  :before_save, :after_save,
18
- :before_destroy, :after_destroy].each do |callback|
18
+ :before_destroy, :after_destroy,
19
+ :validate, :validate_on_create, :validate_on_update].each do |callback|
19
20
  callback_method = "#{callback}_callback"
20
21
  send(callback, callback_method)
21
22
  define_method(callback_method) do
@@ -36,7 +37,7 @@ class CallbacksTest < Test::Unit::TestCase
36
37
 
37
38
  should "get the order right for creating documents" do
38
39
  doc = @document.create(:name => 'John Nunemaker')
39
- doc.history.should == [:before_validation_on_create, :before_validation, :after_validation, :before_save, :before_create, :after_create, :after_save]
40
+ doc.history.should == [:before_validation, :before_validation_on_create, :validate, :validate_on_create, :after_validation, :before_save, :before_create, :after_create, :after_save]
40
41
  end
41
42
 
42
43
  should "get the order right for updating documents" do
@@ -44,7 +45,7 @@ class CallbacksTest < Test::Unit::TestCase
44
45
  doc.clear_history
45
46
  doc.name = 'John'
46
47
  doc.save
47
- doc.history.should == [:before_validation_on_update, :before_validation, :after_validation, :before_save, :before_update, :after_update, :after_save]
48
+ doc.history.should == [:before_validation, :before_validation_on_update, :validate, :validate_on_update, :after_validation, :before_save, :before_update, :after_update, :after_save]
48
49
  end
49
50
 
50
51
  should "work for before and after validation" do
@@ -56,6 +56,19 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
56
56
  doc.attributes[:name].should == 'new value'
57
57
  doc.attributes[:foobar].should be(nil)
58
58
  end
59
+
60
+ should "not ignore keys that have methods defined" do
61
+ @document.class_eval do
62
+ attr_writer :password
63
+
64
+ def passwd
65
+ @password
66
+ end
67
+ end
68
+
69
+ doc = @document.new(:name => 'foobar', :password => 'secret')
70
+ doc.passwd.should == 'secret'
71
+ end
59
72
 
60
73
  should "typecast key values" do
61
74
  doc = @document.new(:name => 1234, :age => '21')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jnunemaker-mongomapper
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
  - John Nunemaker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-28 00:00:00 -07:00
12
+ date: 2009-07-03 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency