mongo_mapper 0.6.8 → 0.6.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -17
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/mongo_mapper/associations/base.rb +19 -10
- data/lib/mongo_mapper/associations/in_array_proxy.rb +137 -0
- data/lib/mongo_mapper/associations/one_proxy.rb +61 -0
- data/lib/mongo_mapper/associations/proxy.rb +0 -2
- data/lib/mongo_mapper/associations.rb +5 -3
- data/lib/mongo_mapper/callbacks.rb +30 -78
- data/lib/mongo_mapper/dirty.rb +5 -24
- data/lib/mongo_mapper/document.rb +117 -144
- data/lib/mongo_mapper/embedded_document.rb +7 -11
- data/lib/mongo_mapper/finder_options.rb +42 -30
- data/lib/mongo_mapper/mongo_mapper.rb +125 -0
- data/lib/mongo_mapper/pagination.rb +12 -1
- data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +1 -0
- data/lib/mongo_mapper/serialization.rb +2 -2
- data/lib/mongo_mapper/serializers/json_serializer.rb +2 -46
- data/lib/mongo_mapper/support.rb +5 -2
- data/lib/mongo_mapper/validations.rb +1 -3
- data/lib/mongo_mapper.rb +8 -2
- data/mongo_mapper.gemspec +14 -8
- data/specs.watchr +3 -5
- data/test/functional/associations/test_belongs_to_proxy.rb +43 -0
- data/test/functional/associations/test_in_array_proxy.rb +309 -0
- data/test/functional/associations/test_many_documents_proxy.rb +103 -53
- data/test/functional/associations/test_many_polymorphic_proxy.rb +4 -3
- data/test/functional/associations/test_one_proxy.rb +131 -0
- data/test/functional/test_binary.rb +15 -0
- data/test/functional/test_document.rb +581 -631
- data/test/functional/test_modifiers.rb +242 -0
- data/test/functional/test_validations.rb +0 -17
- data/test/models.rb +1 -1
- data/test/support/timing.rb +1 -1
- data/test/unit/associations/test_base.rb +54 -13
- data/test/unit/test_document.rb +32 -0
- data/test/unit/test_embedded_document.rb +0 -9
- data/test/unit/test_finder_options.rb +36 -7
- data/test/unit/test_pagination.rb +6 -0
- data/test/unit/test_rails_compatibility.rb +4 -1
- data/test/unit/test_support.rb +4 -0
- metadata +12 -6
- data/lib/mongo_mapper/observing.rb +0 -50
- data/test/unit/test_observing.rb +0 -101
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'observer'
|
2
|
-
require 'singleton'
|
3
|
-
require 'set'
|
4
|
-
|
5
|
-
module MongoMapper
|
6
|
-
module Observing #:nodoc:
|
7
|
-
def self.included(model)
|
8
|
-
model.class_eval do
|
9
|
-
extend Observable
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class Observer
|
15
|
-
include Singleton
|
16
|
-
|
17
|
-
class << self
|
18
|
-
def observe(*models)
|
19
|
-
models.flatten!
|
20
|
-
models.collect! { |model| model.is_a?(Symbol) ? model.to_s.camelize.constantize : model }
|
21
|
-
define_method(:observed_classes) { Set.new(models) }
|
22
|
-
end
|
23
|
-
|
24
|
-
def observed_class
|
25
|
-
if observed_class_name = name[/(.*)Observer/, 1]
|
26
|
-
observed_class_name.constantize
|
27
|
-
else
|
28
|
-
nil
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def initialize
|
34
|
-
Set.new(observed_classes).each { |klass| add_observer! klass }
|
35
|
-
end
|
36
|
-
|
37
|
-
def update(observed_method, object) #:nodoc:
|
38
|
-
send(observed_method, object) if respond_to?(observed_method)
|
39
|
-
end
|
40
|
-
|
41
|
-
protected
|
42
|
-
def observed_classes
|
43
|
-
Set.new([self.class.observed_class].compact.flatten)
|
44
|
-
end
|
45
|
-
|
46
|
-
def add_observer!(klass)
|
47
|
-
klass.add_observer(self)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
data/test/unit/test_observing.rb
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class Comment
|
4
|
-
include MongoMapper::Document
|
5
|
-
|
6
|
-
key :name, String
|
7
|
-
key :body, String
|
8
|
-
|
9
|
-
attr_accessor :callers
|
10
|
-
before_validation :record_callers
|
11
|
-
|
12
|
-
def after_validation
|
13
|
-
record_callers
|
14
|
-
end
|
15
|
-
|
16
|
-
def record_callers
|
17
|
-
callers << self.class if callers
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class Article
|
22
|
-
include MongoMapper::Document
|
23
|
-
|
24
|
-
key :title, String
|
25
|
-
key :body, String
|
26
|
-
end
|
27
|
-
|
28
|
-
class CommentObserver < MongoMapper::Observer
|
29
|
-
attr_accessor :callers
|
30
|
-
|
31
|
-
def after_validation(model)
|
32
|
-
callers << self.class if callers
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class AuditObserver < MongoMapper::Observer
|
37
|
-
observe Article, Comment
|
38
|
-
attr_reader :document
|
39
|
-
|
40
|
-
def after_validation(document)
|
41
|
-
@document = document
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
class GlobalObserver < MongoMapper::Observer
|
46
|
-
observe Article, Comment
|
47
|
-
attr_reader :document
|
48
|
-
|
49
|
-
def before_save(document)
|
50
|
-
@document = document
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
class NonAutomaticObserver < MongoMapper::Observer
|
55
|
-
observe Comment
|
56
|
-
attr_reader :comment
|
57
|
-
|
58
|
-
def after_validation(comment)
|
59
|
-
@comment = comment
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
class ObserverTest < Test::Unit::TestCase
|
64
|
-
should "fire model callbacks before observer" do
|
65
|
-
callers = []
|
66
|
-
comment = Comment.new
|
67
|
-
comment.callers = callers
|
68
|
-
|
69
|
-
CommentObserver.instance.callers = callers
|
70
|
-
|
71
|
-
comment.valid?
|
72
|
-
callers.should == [Comment, Comment, CommentObserver]
|
73
|
-
end
|
74
|
-
|
75
|
-
should "automatically observe model based on name when possible" do
|
76
|
-
CommentObserver.observed_class.should == Comment
|
77
|
-
end
|
78
|
-
|
79
|
-
should "be able to observe other models using observe" do
|
80
|
-
obs = NonAutomaticObserver.instance
|
81
|
-
comment = Comment.new(:name => 'John Nunemaker', :body => 'is awesome')
|
82
|
-
comment.valid?
|
83
|
-
obs.comment.name.should == 'John Nunemaker'
|
84
|
-
obs.comment.body.should == 'is awesome'
|
85
|
-
end
|
86
|
-
|
87
|
-
should "be able to observe multiple models" do
|
88
|
-
obs = AuditObserver.instance
|
89
|
-
comment = Comment.new(:name => 'Steve Smith', :body => 'is awesome')
|
90
|
-
comment.valid?
|
91
|
-
|
92
|
-
obs.document.name.should == 'Steve Smith'
|
93
|
-
obs.document.body.should == 'is awesome'
|
94
|
-
|
95
|
-
article = Article.new(:title => 'Ordered List Is Awesome', :body => 'Learn to accept it!')
|
96
|
-
article.valid?
|
97
|
-
|
98
|
-
obs.document.title.should == 'Ordered List Is Awesome'
|
99
|
-
obs.document.body.should == 'Learn to accept it!'
|
100
|
-
end
|
101
|
-
end
|