mongo_mapper-unstable 2009.12.14 → 2009.12.16

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ Jeweler::Tasks.new do |gem|
12
12
  gem.authors = ["John Nunemaker"]
13
13
 
14
14
  gem.add_dependency('activesupport', '>= 2.3')
15
- gem.add_dependency('mongo', '0.18')
15
+ gem.add_dependency('mongo', '0.18.1')
16
16
  gem.add_dependency('jnunemaker-validatable', '1.8.1')
17
17
 
18
18
  gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2009.12.14
1
+ 2009.12.16
data/lib/mongo_mapper.rb CHANGED
@@ -1,3 +1,13 @@
1
+ # if Gem is defined i'll assume you are using rubygems and lock specific versions
2
+ # call me crazy but a plain old require will just get the latest version you have installed
3
+ # so i want to make sure that if you are using gems you do in fact have the correct versions
4
+ # if there is a better way to do this, please enlighten me!
5
+ if self.class.const_defined?(:Gem)
6
+ gem 'activesupport', '>= 2.3'
7
+ gem 'mongo', '0.18.1'
8
+ gem 'jnunemaker-validatable', '1.8.1'
9
+ end
10
+
1
11
  require 'active_support'
2
12
  require 'mongo'
3
13
  require 'validatable'
@@ -1,140 +1,34 @@
1
1
  module MongoMapper
2
2
  module Associations
3
3
  module ClassMethods
4
- ##
5
- # This macro allows you define a "belongs-to" relationship between one
6
- # document and some other document.
7
- #
8
- # == Requirements
9
- #
10
- # Usage of this macro requires that your document define a key that can
11
- # be used to store the ID of the target document that is the parent of
12
- # this document.
13
- #
14
- # == Conventions
15
- #
16
- # The following is a list of the conventions used by MongoMapper in
17
- # defining a belongs-to relationship. Each can likely be overridden via
18
- # the +options+ parameter.
19
- #
20
- # * The name of your belongs-to association is the lowercase, singular
21
- # name of the target class
22
- # * A key with the name of your association exists with an "_id" suffix
23
- # to store the ID of the target of this relationship
24
- #
25
- # @param [Symbol] association_id The name of this association
26
- # @param [Hash] options Optional parameters that define the
27
- # characteristics of this relationship. These are often used to
28
- # override MongoMapper conventions.
29
- # @option options [Boolean] :polymorphic (false) Set this option to
30
- # <code>true</code> to define a relationship that can be between this
31
- # document and any other type of document. Note that you *must* also
32
- # have a key on your document to store the type of document in this
33
- # relationship.
34
- # @option options [String] :class_name If your relationship doesn't use
35
- # the name of some class, you *must* use this option to indicate the
36
- # target class for this relationship.
37
- # @option options [Symbol] :foreign_key Use this option to specify a
38
- # non-conventional key that stores the ID of the parent in this
39
- # relationship
40
- #
41
- # @example Conventional, and simple, usage of <code>belongs_to</code>
42
- # class Novel
43
- # include MongoMapper::Document
44
- #
45
- # key :author_id, String # our "foreign key"
46
- #
47
- # belongs_to :author
48
- # end
49
- #
50
- # @example Using :foreign_key and :class_name
51
- # class Pet
52
- # include MongoMapper::Document
53
- #
54
- # key :person_id, String
55
- #
56
- # belongs_to :owner,
57
- # :foreign_key => :person_id,
58
- # :class_name => "Person"
59
- # end
60
- #
61
- # @example Defining a polymorphic belongs-to relationship
62
- # class Vehicle
63
- # include MongoMapper::Document
64
- #
65
- # key :owner_id, String
66
- # key :owner_type, String
67
- #
68
- # belongs_to :owner,
69
- # :polymorphic => true
70
- # end
71
- #
72
- # @example Non-standard polymorphic belongs-to relationship
73
- # class Vehicle
74
- # include MongoMapper::Document
75
- #
76
- # key :person_id, String
77
- # key :person_type, String
78
- #
79
- # belongs_to :owner,
80
- # :polymorphic => true,
81
- # :foreign_key => "person_id",
82
- # :type_key_name => "person_type"
83
- # end
84
4
  def belongs_to(association_id, options={}, &extension)
85
5
  create_association(:belongs_to, association_id, options, &extension)
86
6
  self
87
7
  end
88
8
 
89
- ##
90
- # This macro allows you to define a "has-many" relationship between a
91
- # document, and numerous child documents.
92
- #
93
- # == Conventions
94
- #
95
- # The following is a list of the conventions used by MongoMapper in
96
- # defining this relationship. Each can likely be overridden via the
97
- # +options+ parameter.
98
- #
99
- # * The name of your association is the lowercase, *plural* name of the
100
- # target class
101
- # * Your target class must have a "foreign key" bearing the name of this
102
- # class suffixed by "_id"
103
- #
104
- # @param [Symbol] association_id The name of this association
105
- # @param [Hash] options Optional parameters that define the
106
- # characteristics of this relationship. These are often used to
107
- # override MongoMapper conventions.
108
- # @option options [String] :class_name If your relationship doesn't use
109
- # the name of some class, you *must* use this option to indicate the
110
- # target class for this relationship.
111
- # @option options [Symbol] :foreign_key Use this option to specify a
112
- # non-conventional key that stores the ID of the parent in this
113
- # relationship
114
- # @option options [#to_s] :as Used when the target relationship is
115
- # polymorphic (i.e. the +belongs_to+ has set <tt>:polymorphic</tt> to
116
- # +true+). See examples for usage.
117
9
  def many(association_id, options={}, &extension)
118
10
  create_association(:many, association_id, options, &extension)
119
11
  self
120
12
  end
121
-
13
+
122
14
  def associations
123
- @associations ||= self.superclass.respond_to?(:associations) ?
124
- self.superclass.associations :
125
- HashWithIndifferentAccess.new
15
+ @associations ||= HashWithIndifferentAccess.new
16
+ end
17
+
18
+ def associations=(hash)
19
+ @associations = hash
20
+ end
21
+
22
+ def inherited(subclass)
23
+ subclass.associations = associations.dup
24
+ super
126
25
  end
127
26
 
128
27
  private
129
28
  def create_association(type, name, options, &extension)
130
29
  association = Associations::Base.new(type, name, options, &extension)
131
30
  associations[association.name] = association
132
- define_association_methods(association)
133
- define_dependent_callback(association)
134
- association
135
- end
136
-
137
- def define_association_methods(association)
31
+
138
32
  define_method(association.name) do
139
33
  get_proxy(association)
140
34
  end
@@ -143,39 +37,32 @@ module MongoMapper
143
37
  get_proxy(association).replace(value)
144
38
  value
145
39
  end
146
- end
147
-
148
- def define_dependent_callback(association)
149
- if association.options[:dependent]
150
- if association.many?
151
- define_dependent_callback_for_many(association)
152
- end
153
- end
154
- end
155
-
156
- def define_dependent_callback_for_many(association)
157
- after_destroy do |doc|
158
- if !association.embeddable?
40
+
41
+ if association.options[:dependent] && association.many? && !association.embeddable?
42
+ after_destroy do |doc|
159
43
  case association.options[:dependent]
160
- when :destroy
161
- doc.get_proxy(association).destroy_all
162
- when :delete_all
163
- doc.get_proxy(association).delete_all
164
- when :nullify
165
- doc.get_proxy(association).nullify
44
+ when :destroy
45
+ doc.get_proxy(association).destroy_all
46
+ when :delete_all
47
+ doc.get_proxy(association).delete_all
48
+ when :nullify
49
+ doc.get_proxy(association).nullify
166
50
  end
167
51
  end
168
52
  end
169
53
  end
170
54
  end
171
-
55
+
172
56
  module InstanceMethods
57
+ def associations
58
+ self.class.associations
59
+ end
60
+
173
61
  def get_proxy(association)
174
62
  unless proxy = self.instance_variable_get(association.ivar)
175
63
  proxy = association.proxy_class.new(self, association)
176
64
  self.instance_variable_set(association.ivar, proxy) if !frozen?
177
65
  end
178
-
179
66
  proxy
180
67
  end
181
68
  end
@@ -67,8 +67,8 @@ module MongoMapper
67
67
  end
68
68
 
69
69
  # <tt>reload</tt> the record and clears changed keys.
70
- # def reload_with_dirty(*args) #:nodoc:
71
- # record = reload_without_dirty(*args)
70
+ # def reload(*args) #:nodoc:
71
+ # record = super
72
72
  # changed_keys.clear
73
73
  # record
74
74
  # end
@@ -427,7 +427,7 @@ module MongoMapper
427
427
 
428
428
  def reload
429
429
  doc = self.class.find(_id)
430
- self.class.associations.each { |name, assoc| send(name).reset }
430
+ self.class.associations.each { |name, assoc| send(name).reset if respond_to?(name) }
431
431
  self.attributes = doc.attributes
432
432
  self
433
433
  end
@@ -1,12 +1,10 @@
1
- require 'observer'
2
-
3
1
  module MongoMapper
4
2
  module EmbeddedDocument
5
3
  def self.included(model)
6
4
  model.class_eval do
7
5
  extend ClassMethods
8
6
  include InstanceMethods
9
-
7
+
10
8
  extend Associations::ClassMethods
11
9
  include Associations::InstanceMethods
12
10
 
@@ -81,14 +79,9 @@ module MongoMapper
81
79
  instance.to_mongo
82
80
  end
83
81
 
84
- def from_mongo(instance_or_hash)
85
- return nil if instance_or_hash.nil?
86
-
87
- if instance_or_hash.is_a?(self)
88
- instance_or_hash
89
- else
90
- initialize_doc(instance_or_hash)
91
- end
82
+ def from_mongo(value)
83
+ return nil if value.nil?
84
+ value.is_a?(self) ? value : initialize_doc(value)
92
85
  end
93
86
 
94
87
  private
@@ -179,13 +172,9 @@ module MongoMapper
179
172
  end
180
173
 
181
174
  module InstanceMethods
182
- def logger
183
- self.class.logger
184
- end
185
-
186
175
  def initialize(attrs={})
187
176
  unless attrs.nil?
188
- self.class.associations.each_pair do |name, association|
177
+ associations.each do |name, association|
189
178
  if collection = attrs.delete(name)
190
179
  if association.many? && association.klass.embeddable?
191
180
  root_document = attrs[:_root_document] || self
@@ -337,6 +326,10 @@ module MongoMapper
337
326
  self.attributes = attrs
338
327
  save!
339
328
  end
329
+
330
+ def logger
331
+ self.class.logger
332
+ end
340
333
 
341
334
  private
342
335
  def _keys
@@ -380,7 +373,7 @@ module MongoMapper
380
373
  end
381
374
 
382
375
  def embedded_associations
383
- self.class.associations.select do |name, association|
376
+ associations.select do |name, association|
384
377
  association.embeddable?
385
378
  end.map do |name, association|
386
379
  association
data/mongo_mapper.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongo_mapper}
8
- s.version = "0.6.7"
8
+ s.version = "0.6.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Nunemaker"]
12
- s.date = %q{2009-12-13}
12
+ s.date = %q{2009-12-15}
13
13
  s.default_executable = %q{mmconsole}
14
14
  s.email = %q{nunemaker@gmail.com}
15
15
  s.executables = ["mmconsole"]
@@ -145,7 +145,7 @@ Gem::Specification.new do |s|
145
145
 
146
146
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
147
147
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
148
- s.add_runtime_dependency(%q<mongo>, ["= 0.18"])
148
+ s.add_runtime_dependency(%q<mongo>, ["= 0.18.1"])
149
149
  s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
150
150
  s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
151
151
  s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -153,7 +153,7 @@ Gem::Specification.new do |s|
153
153
  s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
154
154
  else
155
155
  s.add_dependency(%q<activesupport>, [">= 2.3"])
156
- s.add_dependency(%q<mongo>, ["= 0.18"])
156
+ s.add_dependency(%q<mongo>, ["= 0.18.1"])
157
157
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
158
158
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
159
159
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -162,7 +162,7 @@ Gem::Specification.new do |s|
162
162
  end
163
163
  else
164
164
  s.add_dependency(%q<activesupport>, [">= 2.3"])
165
- s.add_dependency(%q<mongo>, ["= 0.18"])
165
+ s.add_dependency(%q<mongo>, ["= 0.18.1"])
166
166
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
167
167
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
168
168
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -54,15 +54,15 @@ class DirtyTest < Test::Unit::TestCase
54
54
 
55
55
  should "not happen when loading from database" do
56
56
  doc = @document.create(:phrase => 'Foo')
57
-
58
- doc = doc.reload
57
+ doc.phrase = 'Fart'
58
+ doc.changed?.should be_true
59
+ doc.reload
59
60
  doc.changed?.should be_false
60
61
  end
61
62
 
62
63
  should "happen if changed after loading from database" do
63
64
  doc = @document.create(:phrase => 'Foo')
64
-
65
- doc = doc.reload
65
+ doc.reload
66
66
  doc.changed?.should be_false
67
67
  doc.phrase = 'Bar'
68
68
  doc.changed?.should be_true
@@ -962,6 +962,8 @@ class DocumentTest < Test::Unit::TestCase
962
962
  class ::DocDaughter < ::DocParent; end
963
963
  class ::DocSon < ::DocParent; end
964
964
  class ::DocGrandSon < ::DocSon; end
965
+
966
+ DocSon.many :children, :class_name => 'DocGrandSon'
965
967
 
966
968
  @parent = DocParent.new({:name => "Daddy Warbucks"})
967
969
  @daughter = DocDaughter.new({:name => "Little Orphan Annie"})
@@ -1100,6 +1102,13 @@ class DocumentTest < Test::Unit::TestCase
1100
1102
  DocParent.delete_all
1101
1103
  }.should change { DocParent.count }.by(-2)
1102
1104
  end
1105
+
1106
+ should "be able to reload parent inherited class" do
1107
+ brian = DocParent.create(:name => 'Brian')
1108
+ brian.name = 'B-Dawg'
1109
+ brian.reload
1110
+ brian.name.should == 'Brian'
1111
+ end
1103
1112
  end
1104
1113
 
1105
1114
  context "timestamping" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper-unstable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2009.12.14
4
+ version: 2009.12.16
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-12-14 00:00:00 -05:00
12
+ date: 2009-12-16 00:00:00 -05:00
13
13
  default_executable: mmconsole
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - "="
32
32
  - !ruby/object:Gem::Version
33
- version: "0.18"
33
+ version: 0.18.1
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: jnunemaker-validatable