djsun-mongo_mapper 0.5.5.3 → 0.5.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -35,7 +35,10 @@ You can also just declare the source:
35
35
 
36
36
  == Documentation
37
37
 
38
+ Documentation is lacking right now because if you can't look through the code right now and feel comfortable, this is probably too young for you to use. Wait til it stabilizes a bit more.
39
+
38
40
  http://rdoc.info/projects/jnunemaker/mongomapper
41
+ http://groups.google.com/group/mongomapper
39
42
 
40
43
  == More Info
41
44
 
data/Rakefile CHANGED
@@ -1,89 +1,50 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
3
 
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "djsun-mongo_mapper"
8
- gem.summary = %Q{Awesome gem for modeling your domain and storing it in mongo}
9
- gem.email = "nunemaker@gmail.com"
10
- gem.homepage = "http://github.com/jnunemaker/mongomapper"
11
- gem.authors = ["John Nunemaker"]
12
- gem.rubyforge_project = "mongomapper"
13
-
14
- gem.add_dependency('activesupport', '>= 2.3')
15
- gem.add_dependency('mongo', '0.15.1')
16
- gem.add_dependency('jnunemaker-validatable', '1.7.4')
17
-
18
- gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
19
- gem.add_development_dependency('shoulda', '2.10.2')
20
- gem.add_development_dependency('timecop', '0.3.1')
21
- gem.add_development_dependency('mocha', '0.9.4')
22
- end
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "djsun-mongo_mapper"
7
+ gem.summary = %Q{Awesome gem for modeling your domain and storing it in mongo}
8
+ gem.description = %Q{Awesome gem for modeling your domain and storing it in mongo}
9
+ gem.email = "nunemaker@gmail.com"
10
+ gem.homepage = "http://github.com/jnunemaker/mongomapper"
11
+ gem.authors = ["John Nunemaker"]
23
12
 
24
- Jeweler::RubyforgeTasks.new do |rubyforge|
25
- rubyforge.doc_task = "rdoc"
26
- end
27
- rescue LoadError
28
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
13
+ gem.add_dependency('activesupport', '>= 2.3')
14
+ gem.add_dependency('mongo', '0.15.1')
15
+ gem.add_dependency('jnunemaker-validatable', '1.8.0')
16
+
17
+ gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
18
+ gem.add_development_dependency('shoulda', '2.10.2')
19
+ gem.add_development_dependency('timecop', '0.3.1')
20
+ gem.add_development_dependency('mocha', '0.9.4')
29
21
  end
30
22
 
23
+ Jeweler::GemcutterTasks.new
24
+
31
25
  require 'rake/testtask'
32
26
  Rake::TestTask.new(:test) do |test|
33
- test.libs << 'lib' << 'test'
27
+ test.libs << 'test'
28
+ test.ruby_opts << '-rubygems'
34
29
  test.pattern = 'test/**/test_*.rb'
35
30
  test.verbose = true
36
31
  end
37
32
 
38
33
  namespace :test do
39
34
  Rake::TestTask.new(:units) do |test|
40
- test.libs << 'lib' << 'test'
35
+ test.libs << 'test'
36
+ test.ruby_opts << '-rubygems'
41
37
  test.pattern = 'test/unit/**/test_*.rb'
42
38
  test.verbose = true
43
39
  end
44
40
 
45
41
  Rake::TestTask.new(:functionals) do |test|
46
- test.libs << 'lib' << 'test'
47
- test.pattern = 'test/functional/**/test_*.rb'
48
- test.verbose = true
49
- end
50
- end
51
-
52
- begin
53
- require 'rcov/rcovtask'
54
- Rcov::RcovTask.new do |test|
55
42
  test.libs << 'test'
56
- test.pattern = 'test/**/test_*.rb'
43
+ test.ruby_opts << '-rubygems'
44
+ test.pattern = 'test/functional/**/test_*.rb'
57
45
  test.verbose = true
58
46
  end
59
- rescue LoadError
60
- task :rcov do
61
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
62
- end
63
- end
64
-
65
- begin
66
- require 'cucumber/rake/task'
67
- Cucumber::Rake::Task.new(:features)
68
- rescue LoadError
69
- task :features do
70
- abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
71
- end
72
47
  end
73
48
 
74
- task :default => :test
75
-
76
- require 'rake/rdoctask'
77
- Rake::RDocTask.new do |rdoc|
78
- if File.exist?('VERSION.yml')
79
- config = YAML.load(File.read('VERSION.yml'))
80
- version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
81
- else
82
- version = ""
83
- end
84
-
85
- rdoc.rdoc_dir = 'rdoc'
86
- rdoc.title = "MongoMapper #{version}"
87
- rdoc.rdoc_files.include('README*')
88
- rdoc.rdoc_files.include('lib/**/*.rb')
89
- end
49
+ task :default => :test
50
+ task :test => :check_dependencies
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.5.3
1
+ 0.5.6.1
@@ -1,16 +1,17 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{djsun-mongo_mapper}
8
- s.version = "0.5.5.3"
8
+ s.version = "0.5.6.1"
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-10-19}
12
+ s.date = %q{2009-10-26}
13
13
  s.default_executable = %q{mmconsole}
14
+ s.description = %q{Awesome gem for modeling your domain and storing it in mongo}
14
15
  s.email = %q{nunemaker@gmail.com}
15
16
  s.executables = ["mmconsole"]
16
17
  s.extra_rdoc_files = [
@@ -96,7 +97,6 @@ Gem::Specification.new do |s|
96
97
  s.homepage = %q{http://github.com/jnunemaker/mongomapper}
97
98
  s.rdoc_options = ["--charset=UTF-8"]
98
99
  s.require_paths = ["lib"]
99
- s.rubyforge_project = %q{mongomapper}
100
100
  s.rubygems_version = %q{1.3.5}
101
101
  s.summary = %q{Awesome gem for modeling your domain and storing it in mongo}
102
102
  s.test_files = [
@@ -146,7 +146,7 @@ Gem::Specification.new do |s|
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
148
  s.add_runtime_dependency(%q<mongo>, ["= 0.15.1"])
149
- s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.7.4"])
149
+ s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.0"])
150
150
  s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
151
151
  s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
152
152
  s.add_development_dependency(%q<timecop>, ["= 0.3.1"])
@@ -154,7 +154,7 @@ Gem::Specification.new do |s|
154
154
  else
155
155
  s.add_dependency(%q<activesupport>, [">= 2.3"])
156
156
  s.add_dependency(%q<mongo>, ["= 0.15.1"])
157
- s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.4"])
157
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.0"])
158
158
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
159
159
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
160
160
  s.add_dependency(%q<timecop>, ["= 0.3.1"])
@@ -163,10 +163,11 @@ Gem::Specification.new do |s|
163
163
  else
164
164
  s.add_dependency(%q<activesupport>, [">= 2.3"])
165
165
  s.add_dependency(%q<mongo>, ["= 0.15.1"])
166
- s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.4"])
166
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.0"])
167
167
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
168
168
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
169
169
  s.add_dependency(%q<timecop>, ["= 0.3.1"])
170
170
  s.add_dependency(%q<mocha>, ["= 0.9.4"])
171
171
  end
172
172
  end
173
+
data/lib/mongo_mapper.rb CHANGED
@@ -1,10 +1,4 @@
1
- require 'rubygems'
2
-
3
- gem 'activesupport', '>= 2.3'
4
- gem 'mongo', '0.15.1'
5
- gem 'jnunemaker-validatable', '1.7.4'
6
-
7
- require 'activesupport'
1
+ require 'active_support'
8
2
  require 'mongo'
9
3
  require 'validatable'
10
4
 
@@ -60,13 +54,12 @@ module MongoMapper
60
54
  module Finders
61
55
  def dynamic_find(finder, args)
62
56
  attributes = {}
63
- find_options = args.extract_options!.deep_merge(:conditions => attributes)
64
-
65
57
  finder.attributes.each_with_index do |attr, index|
66
58
  attributes[attr] = args[index]
67
59
  end
68
-
69
- result = find(finder.finder, find_options)
60
+
61
+ options = args.extract_options!.merge(attributes)
62
+ result = find(finder.finder, options)
70
63
 
71
64
  if result.nil?
72
65
  if finder.bang
@@ -42,8 +42,6 @@ module MongoMapper
42
42
  if association.options[:dependent]
43
43
  if association.many?
44
44
  define_dependent_callback_for_many(association)
45
- elsif association.belongs_to?
46
- define_dependent_callback_for_belongs_to(association)
47
45
  end
48
46
  end
49
47
  end
@@ -63,14 +61,6 @@ module MongoMapper
63
61
  end
64
62
  end
65
63
 
66
- def define_dependent_callback_for_belongs_to(association)
67
- after_destroy do |doc|
68
- case association.options[:dependent]
69
- when :destroy
70
- doc.get_proxy(association).destroy
71
- end
72
- end
73
- end
74
64
  end
75
65
 
76
66
  module InstanceMethods
@@ -4,7 +4,7 @@ module MongoMapper
4
4
  delegate :klass, :to => :@association
5
5
  delegate :collection, :to => :klass
6
6
 
7
- include MongoMapper::Finders
7
+ include ::MongoMapper::Finders
8
8
 
9
9
  def find(*args)
10
10
  options = args.extract_options!
@@ -27,8 +27,8 @@ module MongoMapper
27
27
  find(:last, scoped_options(options))
28
28
  end
29
29
 
30
- def count(conditions={})
31
- klass.count(conditions.deep_merge(scoped_conditions))
30
+ def count(options={})
31
+ klass.count(options.merge(scoped_conditions))
32
32
  end
33
33
 
34
34
  def replace(docs)
@@ -57,20 +57,20 @@ module MongoMapper
57
57
  doc
58
58
  end
59
59
 
60
- def destroy_all(conditions={})
61
- all(:conditions => conditions).map(&:destroy)
60
+ def destroy_all(options={})
61
+ all(options).map(&:destroy)
62
62
  reset
63
63
  end
64
64
 
65
- def delete_all(conditions={})
66
- klass.delete_all(conditions.deep_merge(scoped_conditions))
65
+ def delete_all(options={})
66
+ klass.delete_all(options.merge(scoped_conditions))
67
67
  reset
68
68
  end
69
69
 
70
70
  def nullify
71
- criteria = FinderOptions.to_mongo_criteria(scoped_conditions)
71
+ criteria = FinderOptions.new(klass, scoped_conditions).criteria
72
72
  all(criteria).each do |doc|
73
- doc.update_attributes self.foreign_key => nil
73
+ doc.update_attributes(self.foreign_key => nil)
74
74
  end
75
75
  reset
76
76
  end
@@ -91,7 +91,7 @@ module MongoMapper
91
91
  end
92
92
 
93
93
  def scoped_options(options)
94
- options.deep_merge({:conditions => scoped_conditions})
94
+ options.merge(scoped_conditions)
95
95
  end
96
96
 
97
97
  def find_target
@@ -55,7 +55,7 @@ module MongoMapper
55
55
  when 0
56
56
  raise DocumentNotFound, "Couldn't find without an ID"
57
57
  when 1
58
- find_one(args[0], options)
58
+ find_one!(options.merge({:_id => args[0]}))
59
59
  else
60
60
  find_some(args, options)
61
61
  end
@@ -63,31 +63,23 @@ module MongoMapper
63
63
  end
64
64
 
65
65
  def paginate(options)
66
- per_page = options.delete(:per_page) || self.per_page
66
+ per_page = options.delete(:per_page) || self.per_page
67
67
  page = options.delete(:page)
68
- total_entries = count(options[:conditions] || {})
69
- collection = Pagination::PaginationProxy.new(total_entries, page, per_page)
68
+ total_entries = count(options)
69
+ pagination = Pagination::PaginationProxy.new(total_entries, page, per_page)
70
70
 
71
- options[:limit] = collection.limit
72
- options[:skip] = collection.skip
73
-
74
- collection.subject = find_every(options)
75
- collection
71
+ options.merge!(:limit => pagination.limit, :skip => pagination.skip)
72
+ pagination.subject = find_every(options)
73
+ pagination
76
74
  end
77
75
 
78
76
  def first(options={})
79
- options.merge!(:limit => 1)
80
- find_every(options)[0]
77
+ find_one(options)
81
78
  end
82
79
 
83
80
  def last(options={})
84
- if options[:order].blank?
85
- raise ':order option must be provided when using last'
86
- end
87
-
88
- options.merge!(:limit => 1)
89
- options[:order] = invert_order_clause(options[:order])
90
- find_every(options)[0]
81
+ raise ':order option must be provided when using last' if options[:order].blank?
82
+ find_one(options.merge(:order => invert_order_clause(options[:order])))
91
83
  end
92
84
 
93
85
  def all(options={})
@@ -95,18 +87,15 @@ module MongoMapper
95
87
  end
96
88
 
97
89
  def find_by_id(id)
98
- criteria = FinderOptions.to_mongo_criteria(:_id => id)
99
- if doc = collection.find_one(criteria)
100
- new(doc)
101
- end
90
+ find_one(:_id => id)
102
91
  end
103
92
 
104
- def count(conditions={})
105
- collection.find(FinderOptions.to_mongo_criteria(conditions)).count
93
+ def count(options={})
94
+ collection.find(to_criteria(options)).count
106
95
  end
107
96
 
108
- def exists?(conditions={})
109
- !count(conditions).zero?
97
+ def exists?(options={})
98
+ !count(options).zero?
110
99
  end
111
100
 
112
101
  def create(*docs)
@@ -123,8 +112,7 @@ module MongoMapper
123
112
  # For updating multiple documents at once:
124
113
  # Person.update({'1' => {:foo => 'bar'}, '2' => {:baz => 'wick'}})
125
114
  def update(*args)
126
- updating_multiple = args.length == 1
127
- if updating_multiple
115
+ if args.length == 1
128
116
  update_multiple(args[0])
129
117
  else
130
118
  id, attributes = args
@@ -133,21 +121,19 @@ module MongoMapper
133
121
  end
134
122
 
135
123
  def delete(*ids)
136
- criteria = FinderOptions.to_mongo_criteria(:_id => ids.flatten)
137
- collection.remove(criteria)
124
+ collection.remove(to_criteria(:_id => ids.flatten))
138
125
  end
139
126
 
140
- def delete_all(conditions={})
141
- criteria = FinderOptions.to_mongo_criteria(conditions)
142
- collection.remove(criteria)
127
+ def delete_all(options={})
128
+ collection.remove(to_criteria(options))
143
129
  end
144
130
 
145
131
  def destroy(*ids)
146
132
  find_some(ids.flatten).each(&:destroy)
147
133
  end
148
134
 
149
- def destroy_all(conditions={})
150
- find(:all, :conditions => conditions).each(&:destroy)
135
+ def destroy_all(options={})
136
+ all(options).each(&:destroy)
151
137
  end
152
138
 
153
139
  def connection(mongo_connection=nil)
@@ -187,10 +173,17 @@ module MongoMapper
187
173
  def timestamps!
188
174
  key :created_at, Time
189
175
  key :updated_at, Time
190
-
191
176
  class_eval { before_save :update_timestamps }
192
177
  end
193
178
 
179
+ def single_collection_inherited?
180
+ keys.has_key?('_type') && single_collection_inherited_superclass?
181
+ end
182
+
183
+ def single_collection_inherited_superclass?
184
+ superclass.respond_to?(:keys) && superclass.keys.has_key?('_type')
185
+ end
186
+
194
187
  protected
195
188
  def method_missing(method, *args)
196
189
  finder = DynamicFinder.new(method)
@@ -204,33 +197,58 @@ module MongoMapper
204
197
  end
205
198
 
206
199
  private
207
- # Initializes each document and yields each initialized document
200
+ def create_indexes_for(key)
201
+ ensure_index key.name if key.options[:index]
202
+ end
203
+
208
204
  def initialize_each(*docs)
209
205
  instances = []
210
206
  docs = [{}] if docs.blank?
211
207
  docs.flatten.each do |attrs|
212
- doc = new(attrs)
208
+ doc = initialize_doc(attrs)
213
209
  yield(doc)
214
210
  instances << doc
215
211
  end
216
212
  instances.size == 1 ? instances[0] : instances
217
213
  end
218
-
219
- def create_indexes_for(key)
220
- ensure_index key.name if key.options[:index]
214
+
215
+ def initialize_doc(doc)
216
+ begin
217
+ klass = doc['_type'].present? ? doc['_type'].constantize : self
218
+ klass.new(doc)
219
+ rescue NameError
220
+ new(doc)
221
+ end
221
222
  end
222
223
 
223
224
  def find_every(options)
224
- criteria, options = FinderOptions.new(options).to_a
225
+ criteria, options = to_finder_options(options)
225
226
  collection.find(criteria, options).to_a.map do |doc|
226
- begin
227
- klass = doc['_type'].present? ? doc['_type'].constantize : self
228
- klass.new(doc)
229
- rescue NameError
230
- new(doc)
231
- end
227
+ initialize_doc(doc)
228
+ end
229
+ end
230
+
231
+ def find_some(ids, options={})
232
+ ids = ids.flatten.compact.uniq
233
+ documents = find_every(options.merge(:_id => ids))
234
+
235
+ if ids.size == documents.size
236
+ documents
237
+ else
238
+ raise DocumentNotFound, "Couldn't find all of the ids (#{ids.to_sentence}). Found #{documents.size}, but was expecting #{ids.size}"
232
239
  end
233
240
  end
241
+
242
+ def find_one(options={})
243
+ criteria, options = to_finder_options(options)
244
+ if doc = collection.find_one(criteria, options)
245
+ initialize_doc(doc)
246
+ end
247
+ end
248
+
249
+ def find_one!(options={})
250
+ find_one(options) || raise(DocumentNotFound, "Document match #{options.inspect} does not exist in #{collection.name} collection")
251
+ end
234
252
 
235
253
  def invert_order_clause(order)
236
254
  order.split(',').map do |order_segment|
@@ -244,25 +262,6 @@ module MongoMapper
244
262
  end.join(',')
245
263
  end
246
264
 
247
- def find_some(ids, options={})
248
- ids = ids.flatten.compact.uniq
249
- documents = find_every(options.deep_merge(:conditions => {'_id' => ids}))
250
-
251
- if ids.size == documents.size
252
- documents
253
- else
254
- raise DocumentNotFound, "Couldn't find all of the ids (#{ids.to_sentence}). Found #{documents.size}, but was expecting #{ids.size}"
255
- end
256
- end
257
-
258
- def find_one(id, options={})
259
- if doc = find_every(options.deep_merge(:conditions => {:_id => id})).first
260
- doc
261
- else
262
- raise DocumentNotFound, "Document with id of #{id} does not exist in collection named #{collection.name}"
263
- end
264
- end
265
-
266
265
  def update_single(id, attrs)
267
266
  if id.blank? || attrs.blank? || !attrs.is_a?(Hash)
268
267
  raise ArgumentError, "Updating a single document requires an id and a hash of attributes"
@@ -282,6 +281,14 @@ module MongoMapper
282
281
  docs.each_pair { |id, attrs| instances << update(id, attrs) }
283
282
  instances
284
283
  end
284
+
285
+ def to_criteria(options={})
286
+ FinderOptions.new(self, options).criteria
287
+ end
288
+
289
+ def to_finder_options(options={})
290
+ FinderOptions.new(self, options).to_a
291
+ end
285
292
  end
286
293
 
287
294
  module InstanceMethods
@@ -303,9 +310,7 @@ module MongoMapper
303
310
 
304
311
  def destroy
305
312
  return false if frozen?
306
-
307
- criteria = FinderOptions.to_mongo_criteria(:_id => id)
308
- collection.remove(criteria) unless new?
313
+ self.class.delete(id) unless new?
309
314
  freeze
310
315
  end
311
316