mongo_mapper 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -4,4 +4,5 @@ coverage
4
4
  rdoc
5
5
  pkg
6
6
  *~
7
- *.gem
7
+ *.gem
8
+ tmp
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.1
@@ -10,7 +10,6 @@ require 'validatable'
10
10
 
11
11
  module MongoMapper
12
12
  DocumentNotFound = Class.new(StandardError)
13
-
14
13
  DocumentNotValid = Class.new(StandardError) do
15
14
  def initialize(document)
16
15
  @document = document
@@ -25,6 +24,10 @@ module MongoMapper
25
24
  def self.connection=(new_connection)
26
25
  @@connection = new_connection
27
26
  end
27
+
28
+ def self.logger
29
+ connection.logger
30
+ end
28
31
 
29
32
  def self.database=(name)
30
33
  @@database = nil
@@ -29,12 +29,20 @@ module MongoMapper
29
29
  module ClassMethods
30
30
  def find(*args)
31
31
  options = args.extract_options!
32
-
33
32
  case args.first
34
- when :first then find_first(options)
35
- when :last then find_last(options)
33
+ when :first then first(options)
34
+ when :last then last(options)
36
35
  when :all then find_every(options)
37
- else find_from_ids(args, options)
36
+ when Array then find_some(args, options)
37
+ else
38
+ case args.size
39
+ when 0
40
+ raise DocumentNotFound, "Couldn't find without an ID"
41
+ when 1
42
+ find_one(args[0], options)
43
+ else
44
+ find_some(args, options)
45
+ end
38
46
  end
39
47
  end
40
48
 
@@ -44,7 +52,7 @@ module MongoMapper
44
52
  total_entries = count(options[:conditions] || {})
45
53
  collection = Pagination::PaginationProxy.new(total_entries, page, per_page)
46
54
 
47
- options[:limit] = collection.limit
55
+ options[:limit] = collection.limit
48
56
  options[:skip] = collection.skip
49
57
 
50
58
  collection.subject = find_every(options)
@@ -52,11 +60,18 @@ module MongoMapper
52
60
  end
53
61
 
54
62
  def first(options={})
55
- find_first(options)
63
+ options.merge!(:limit => 1)
64
+ find_every(options)[0]
56
65
  end
57
66
 
58
67
  def last(options={})
59
- find_last(options)
68
+ if options[:order].blank?
69
+ raise ':order option must be provided when using last'
70
+ end
71
+
72
+ options.merge!(:limit => 1)
73
+ options[:order] = invert_order_clause(options[:order])
74
+ find_every(options)[0]
60
75
  end
61
76
 
62
77
  def all(options={})
@@ -176,21 +191,8 @@ module MongoMapper
176
191
  collection.find(criteria, options).to_a.map { |doc| new(doc) }
177
192
  end
178
193
 
179
- def find_first(options)
180
- options.merge!(:limit => 1)
181
- options[:order] ||= '$natural'
182
- find_every(options)[0]
183
- end
184
-
185
- def find_last(options)
186
- options.merge!(:limit => 1)
187
- options[:order] = invert_order_clause(options)
188
- find_every(options)[0]
189
- end
190
-
191
- def invert_order_clause(options)
192
- return '$natural desc' unless options[:order]
193
- options[:order].split(',').map do |order_segment|
194
+ def invert_order_clause(order)
195
+ order.split(',').map do |order_segment|
194
196
  if order_segment =~ /\sasc/i
195
197
  order_segment.sub /\sasc/i, ' desc'
196
198
  elsif order_segment =~ /\sdesc/i
@@ -202,7 +204,9 @@ module MongoMapper
202
204
  end
203
205
 
204
206
  def find_some(ids, options={})
207
+ ids = ids.flatten.compact.uniq
205
208
  documents = find_every(options.deep_merge(:conditions => {'_id' => ids}))
209
+
206
210
  if ids.size == documents.size
207
211
  documents
208
212
  else
@@ -218,19 +222,6 @@ module MongoMapper
218
222
  end
219
223
  end
220
224
 
221
- def find_from_ids(ids, options={})
222
- ids = ids.flatten.compact.uniq
223
-
224
- case ids.size
225
- when 0
226
- raise(DocumentNotFound, "Couldn't find without an ID")
227
- when 1
228
- find_one(ids[0], options)
229
- else
230
- find_some(ids, options)
231
- end
232
- end
233
-
234
225
  def update_single(id, attrs)
235
226
  if id.blank? || attrs.blank? || !attrs.is_a?(Hash)
236
227
  raise ArgumentError, "Updating a single document requires an id and a hash of attributes"
@@ -154,4 +154,18 @@ class Time
154
154
  Time.parse(value.to_s)
155
155
  end
156
156
  end
157
+ end
158
+
159
+ # duck punch to get access to internal mongo
160
+ # logger instance until my patch goes thorugh
161
+ module Mongo
162
+ class Connection
163
+ def logger
164
+ @options[:logger]
165
+ end
166
+ end
167
+
168
+ class DB
169
+ attr_reader :logger
170
+ end
157
171
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongo_mapper}
8
- s.version = "0.5.0"
8
+ s.version = "0.5.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"]
@@ -52,6 +52,7 @@ Gem::Specification.new do |s|
52
52
  "lib/mongo_mapper/support.rb",
53
53
  "lib/mongo_mapper/validations.rb",
54
54
  "mongo_mapper.gemspec",
55
+ "specs.watchr",
55
56
  "test/NOTE_ON_TESTING",
56
57
  "test/custom_matchers.rb",
57
58
  "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
@@ -66,6 +67,7 @@ Gem::Specification.new do |s|
66
67
  "test/functional/test_callbacks.rb",
67
68
  "test/functional/test_document.rb",
68
69
  "test/functional/test_embedded_document.rb",
70
+ "test/functional/test_logger.rb",
69
71
  "test/functional/test_pagination.rb",
70
72
  "test/functional/test_rails_compatibility.rb",
71
73
  "test/functional/test_validations.rb",
@@ -107,6 +109,7 @@ Gem::Specification.new do |s|
107
109
  "test/functional/test_callbacks.rb",
108
110
  "test/functional/test_document.rb",
109
111
  "test/functional/test_embedded_document.rb",
112
+ "test/functional/test_logger.rb",
110
113
  "test/functional/test_pagination.rb",
111
114
  "test/functional/test_rails_compatibility.rb",
112
115
  "test/functional/test_validations.rb",
@@ -0,0 +1,32 @@
1
+ def run(cmd)
2
+ puts(cmd)
3
+ system(cmd)
4
+ end
5
+
6
+ def run_test_file(file)
7
+ run "ruby -Itest #{file}"
8
+ end
9
+
10
+ def run_all_tests
11
+ run "rake test"
12
+ end
13
+
14
+ def related_test_files(path)
15
+ Dir['test/**/*.rb'].select { |file| file =~ /#{File.basename(path)}/ }
16
+ end
17
+
18
+ watch('test/test_helper\.rb') { run_all_tests }
19
+ watch('test/.*/test_.*\.rb') { |m| run_test_file(m[0]) }
20
+ watch('lib/.*') do |m|
21
+ related_test_files(m[0]).each { |file| run_test_file(file) }
22
+ end
23
+
24
+ # Ctrl-\
25
+ Signal.trap('QUIT') do
26
+ puts " --- Running all tests ---\n\n"
27
+ run_all_tests
28
+ end
29
+
30
+ # Ctrl-C
31
+ Signal.trap('INT') { abort("\n") }
32
+
@@ -158,7 +158,7 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
158
158
  end
159
159
 
160
160
  should "work with order" do
161
- comments = @post.comments.all(:order => '$natural desc')
161
+ comments = @post.comments.all(:order => 'body desc')
162
162
  comments.should == [@comment2, @comment1]
163
163
  end
164
164
  end
@@ -269,7 +269,7 @@ class ManyProxyTest < Test::Unit::TestCase
269
269
  end
270
270
 
271
271
  should "work with conditions" do
272
- status = @project1.statuses.find(:last, :conditions => {:name => 'New'})
272
+ status = @project1.statuses.find(:last, :order => 'position', :conditions => {:name => 'New'})
273
273
  status.should == @brand_new
274
274
  end
275
275
  end
@@ -280,7 +280,7 @@ class ManyProxyTest < Test::Unit::TestCase
280
280
  end
281
281
 
282
282
  should "work with conditions" do
283
- status = @project1.statuses.last(:conditions => {:name => 'New'})
283
+ status = @project1.statuses.last(:order => 'position', :conditions => {:name => 'New'})
284
284
  status.should == @brand_new
285
285
  end
286
286
  end
@@ -1,11 +1,7 @@
1
1
  require 'test_helper'
2
2
  require 'models'
3
3
 
4
- class AssociationsTest < Test::Unit::TestCase
5
- def setup
6
- clear_all_collections
7
- end
8
-
4
+ class AssociationsTest < Test::Unit::TestCase
9
5
  should "allow changing class names" do
10
6
  class AwesomeUser
11
7
  include MongoMapper::Document
@@ -289,6 +289,10 @@ class DocumentTest < Test::Unit::TestCase
289
289
  should "work as array" do
290
290
  @document.find([@doc1.id, @doc2.id]).should == [@doc1, @doc2]
291
291
  end
292
+
293
+ should "return array if array only has one element" do
294
+ @document.find([@doc1.id]).should == [@doc1]
295
+ end
292
296
  end
293
297
 
294
298
  context "with :all" do
@@ -330,9 +334,13 @@ class DocumentTest < Test::Unit::TestCase
330
334
  context "with #last" do
331
335
  should "find last document based on criteria" do
332
336
  @document.last(:order => 'age').should == @doc2
333
- @document.last(:conditions => {:age => 28}).should == @doc2
337
+ @document.last(:order => 'age', :conditions => {:age => 28}).should == @doc2
334
338
  end
335
- end
339
+
340
+ should "raise error if no order provided" do
341
+ lambda { @document.last() }.should raise_error
342
+ end
343
+ end
336
344
 
337
345
  context "with :find_by" do
338
346
  should "find document based on argument" do
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class LoggerTest < Test::Unit::TestCase
4
+ context "with connection that has logger" do
5
+ setup do
6
+ @output = StringIO.new
7
+ @logger = Logger.new(@output)
8
+ MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => @logger)
9
+ end
10
+
11
+ should "be able to get access to that logger" do
12
+ MongoMapper.logger.should == @logger
13
+ end
14
+
15
+ should "be able to log messages" do
16
+ MongoMapper.logger.debug 'testing'
17
+ @output.string.include?('testing').should be_true
18
+ end
19
+ end
20
+ end
@@ -25,5 +25,6 @@ end
25
25
  DefaultDatabase = 'test' unless defined?(DefaultDatabase)
26
26
  AlternateDatabase = 'test2' unless defined?(AlternateDatabase)
27
27
 
28
- MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
28
+ logger = Logger.new(File.expand_path(File.dirname(__FILE__) + '/../tmp/test.log'))
29
+ MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
29
30
  MongoMapper.database = DefaultDatabase
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -106,6 +106,7 @@ files:
106
106
  - lib/mongo_mapper/support.rb
107
107
  - lib/mongo_mapper/validations.rb
108
108
  - mongo_mapper.gemspec
109
+ - specs.watchr
109
110
  - test/NOTE_ON_TESTING
110
111
  - test/custom_matchers.rb
111
112
  - test/functional/associations/test_belongs_to_polymorphic_proxy.rb
@@ -120,6 +121,7 @@ files:
120
121
  - test/functional/test_callbacks.rb
121
122
  - test/functional/test_document.rb
122
123
  - test/functional/test_embedded_document.rb
124
+ - test/functional/test_logger.rb
123
125
  - test/functional/test_pagination.rb
124
126
  - test/functional/test_rails_compatibility.rb
125
127
  - test/functional/test_validations.rb
@@ -182,6 +184,7 @@ test_files:
182
184
  - test/functional/test_callbacks.rb
183
185
  - test/functional/test_document.rb
184
186
  - test/functional/test_embedded_document.rb
187
+ - test/functional/test_logger.rb
185
188
  - test/functional/test_pagination.rb
186
189
  - test/functional/test_rails_compatibility.rb
187
190
  - test/functional/test_validations.rb