mongomatic 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -61,6 +61,12 @@ module Mongomatic
61
61
  def count
62
62
  find.count
63
63
  end
64
+
65
+ def drop
66
+ self.send(:before_drop) if self.respond_to?(:before_drop)
67
+ collection.drop
68
+ self.send(:after_drop) if self.respond_to?(:after_drop)
69
+ end
64
70
  end
65
71
 
66
72
  attr_accessor :removed, :is_new, :errors
@@ -211,4 +217,4 @@ module Mongomatic
211
217
  @doc = v
212
218
  end
213
219
  end
214
- end
220
+ end
@@ -3,5 +3,20 @@ module Mongomatic
3
3
  def full_messages(sep=" ")
4
4
  collect { |e| e.join(sep) }
5
5
  end
6
+
7
+ def on(field, sep=" ")
8
+ ret = []
9
+ self.each do |err|
10
+ ret << err.join(sep) if err.first =~ /^#{field}/
11
+ end
12
+ case ret.size
13
+ when 0
14
+ nil
15
+ when 1
16
+ ret.first
17
+ else
18
+ ret
19
+ end
20
+ end
6
21
  end
7
22
  end
data/test/helper.rb CHANGED
@@ -13,10 +13,23 @@ class Person < Mongomatic::Base
13
13
  include Mongomatic::Expectations::Helper
14
14
  attr_accessor :callback_tests
15
15
 
16
- def self.create_indexes
17
- collection.create_index("name", :unique => true)
16
+ class << self
17
+ attr_accessor :class_callbacks
18
+ def create_indexes
19
+ collection.create_index("name", :unique => true)
20
+ end
21
+
22
+ def before_drop
23
+ self.class_callbacks ||= []
24
+ self.class_callbacks << :before_drop
25
+ end
26
+
27
+ def after_drop
28
+ self.class_callbacks ||= []
29
+ self.class_callbacks << :after_drop
30
+ end
18
31
  end
19
-
32
+
20
33
  def validate
21
34
  self.errors << ["Name", "can't be empty"] if self["name"].blank?
22
35
  end
@@ -184,6 +184,9 @@ class TestMongomatic < Test::Unit::TestCase
184
184
  p.callback_tests = []
185
185
  p.remove
186
186
  assert_equal [:before_remove, :after_remove], p.callback_tests
187
+ Person.class_callbacks = []
188
+ Person.drop
189
+ assert_equal [:before_drop, :after_drop], Person.class_callbacks
187
190
  end
188
191
 
189
192
  should "raise an error on unique index dup insert" do
@@ -447,4 +450,74 @@ class TestMongomatic < Test::Unit::TestCase
447
450
  p.valid?
448
451
  end
449
452
  end
453
+
454
+ should "be able to drop a collection" do
455
+ p = Person.new
456
+ p['name'] = "Jordan"
457
+ p.insert
458
+
459
+ assert !Person.empty?
460
+
461
+ Person.drop
462
+ assert Person.empty?
463
+ end
464
+
465
+ should "be able to use errors.on with two part error messages" do
466
+ p = Person.new
467
+ class << p
468
+ def validate
469
+ expectations do
470
+ be_expected self['name'], ['name', 'cannot be empty']
471
+ be_of_length self['name'], ['name', 'must be at least 3 characters long'], :minimum => 3
472
+ be_a_number self['age'], ['age', 'must be a number']
473
+ end
474
+ end
475
+ end
476
+
477
+ p.valid?
478
+ assert_equal ['name cannot be empty', 'name must be at least 3 characters long'], p.errors.on(:name)
479
+ assert_equal 'age must be a number', p.errors.on('age')
480
+
481
+ p['name'] = 'Jo'
482
+ p['age'] = 21
483
+
484
+ p.valid?
485
+ assert_equal 'name must be at least 3 characters long', p.errors.on('name')
486
+ assert_nil p.errors.on(:age)
487
+
488
+ p['name'] = 'Jordan'
489
+
490
+ p.valid?
491
+ assert_nil p.errors.on(:name)
492
+ end
493
+
494
+ should "be able to use errors.on with one part error messages" do
495
+ p = Person.new
496
+ class << p
497
+ def validate
498
+ expectations do
499
+ be_expected self['name'], 'name cannot be empty'
500
+ be_of_length self['name'], 'name must be at least 3 characters long', :minimum => 3
501
+ be_a_number self['age'], 'age must be a number'
502
+ end
503
+ end
504
+ end
505
+
506
+ p.valid?
507
+ assert_equal ['name cannot be empty', 'name must be at least 3 characters long'], p.errors.on(:name)
508
+ assert_equal 'age must be a number', p.errors.on('age')
509
+
510
+ p['name'] = 'Jo'
511
+ p['age'] = 21
512
+
513
+ p.valid?
514
+ assert_equal 'name must be at least 3 characters long', p.errors.on('name')
515
+ assert_nil p.errors.on(:age)
516
+
517
+ p['name'] = 'Jordan'
518
+
519
+ p.valid?
520
+ assert_nil p.errors.on(:name)
521
+ end
522
+
450
523
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
8
- - 1
9
- version: 0.4.1
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-24 00:00:00 -07:00
17
+ date: 2010-08-30 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency