mongoa 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
@@ -3,6 +3,7 @@ require 'mongoa/mongo_mapper/matchers'
3
3
  module RSpec
4
4
  module Matchers
5
5
  include Mongoa::MongoMapper::Matchers
6
+ # include Mongoa::MongoMapper::ValidateMatcher
6
7
  end
7
8
  end
8
9
 
@@ -27,6 +27,7 @@ module Mongoa
27
27
  return false
28
28
  end
29
29
  @subject = subject
30
+
30
31
  result = (association_exists? && macro_correct?)
31
32
  result = foreign_key_exists? if result && macro == :belongs_to && !subject.class.embeddable?
32
33
  result
@@ -1,5 +1,6 @@
1
- require 'mongoa/mongo_mapper/associations'
1
+ require 'mongoa/mongo_mapper/improvements/mongo_mapper_associations'
2
2
  require 'mongoa/mongo_mapper/association_matcher'
3
+ require 'mongoa/mongo_mapper/validations'
3
4
 
4
5
  module Mongoa
5
6
  module MongoMapper
@@ -1,47 +1,44 @@
1
1
  module Mongoa
2
2
  module MongoMapper
3
3
  module Matchers
4
-
5
4
  def validate_presence_of(attr)
6
- ValidatePresenceOfMatcher.new(attr)
5
+ ValidateMatcher.new("ValidatesPresenceOf", attr)
7
6
  end
7
+ end
8
8
 
9
- class ValidatePresenceOfMatcher < ValidationMatcher
10
-
11
- def with_message(message)
12
- @expected_message = message if message
13
- self
14
- end
15
-
16
- def matches?(subject)
17
- super(subject)
18
- @expected_message ||= :blank
19
- disallows_value_of(blank_value, @expected_message)
20
- end
9
+ class ValidateMatcher
10
+ attr_reader :validation_type
11
+ attr_reader :attribute
12
+
13
+ def initialize(validation_type, attribute)
14
+ @validation_type = validation_type
15
+ @attribute = attribute
16
+ end
21
17
 
22
- def description
23
- "require #{@attribute} to be set"
24
- end
18
+ def matches?(subject)
19
+ @subject = subject
20
+ attr_validations = model_class.validations.select { |validation| validation.attribute == attribute }
21
+ return false unless attr_validations
22
+ attr_validations.detect { |validation| validation.key.include?(validation_type) }
23
+ end
25
24
 
26
- private
25
+ def description
26
+ "require #{@attribute} to be set"
27
+ end
27
28
 
28
- def blank_value
29
- if collection?
30
- []
31
- else
32
- nil
33
- end
34
- end
29
+ def failure_message
30
+ "#{@attribute} to be a required field (validates_presence_of) but was not"
31
+ end
35
32
 
36
- def collection?
37
- if reflection = @subject.class.reflect_on_association(@attribute)
38
- [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
39
- else
40
- false
41
- end
42
- end
33
+ def negative_failure_message
34
+ "#{@attribute} to not be a required field (validates_presence_of), but it was"
43
35
  end
44
36
 
37
+ private
38
+
39
+ def model_class
40
+ @subject.class
41
+ end
45
42
  end
46
43
  end
47
44
  end
data/mongoa.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoa}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Scott J. Tamosunas"]
12
- s.date = %q{2010-08-09}
12
+ s.date = %q{2010-08-11}
13
13
  s.description = %q{Adds the association and validation macros for Rspec in the same way Shoulda does for ActiveRecord.}
14
14
  s.email = %q{tamosunas@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,13 +26,14 @@ Gem::Specification.new do |s|
26
26
  "lib/mongoa.rb",
27
27
  "lib/mongoa/integrations/rspec2.rb",
28
28
  "lib/mongoa/mongo_mapper/association_matcher.rb",
29
- "lib/mongoa/mongo_mapper/associations.rb",
29
+ "lib/mongoa/mongo_mapper/improvements/mongo_mapper_associations.rb",
30
30
  "lib/mongoa/mongo_mapper/matchers.rb",
31
31
  "lib/mongoa/mongo_mapper/validations.rb",
32
32
  "mongoa.gemspec",
33
- "spec/mongoa_spec.rb",
33
+ "spec/association_matcher_spec.rb",
34
34
  "spec/spec.opts",
35
- "spec/spec_helper.rb"
35
+ "spec/spec_helper.rb",
36
+ "spec/validation_matcher_spec.rb"
36
37
  ]
37
38
  s.homepage = %q{http://github.com/scotttam/mongoa}
38
39
  s.rdoc_options = ["--charset=UTF-8"]
@@ -40,8 +41,9 @@ Gem::Specification.new do |s|
40
41
  s.rubygems_version = %q{1.3.7}
41
42
  s.summary = %q{Like Shoulda but for MongoMapper.}
42
43
  s.test_files = [
43
- "spec/mongoa_spec.rb",
44
- "spec/spec_helper.rb"
44
+ "spec/association_matcher_spec.rb",
45
+ "spec/spec_helper.rb",
46
+ "spec/validation_matcher_spec.rb"
45
47
  ]
46
48
 
47
49
  if s.respond_to? :specification_version then
@@ -0,0 +1,81 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Post
4
+ include MongoMapper::Document
5
+ key :name, String
6
+
7
+ has_many :comments
8
+ has_many :commentwoutfks
9
+ has_many :embedded_comment_no_specifications
10
+ end
11
+
12
+ class PostWOutHM
13
+ include MongoMapper::Document
14
+ key :name, String
15
+ end
16
+
17
+ class Comment
18
+ include MongoMapper::Document
19
+ key :description, String
20
+ key :post_id, ObjectId
21
+
22
+ belongs_to :post
23
+ end
24
+
25
+ class EmbeddedComment
26
+ include MongoMapper::EmbeddedDocument
27
+ key :description, String
28
+
29
+ belongs_to :post
30
+ end
31
+
32
+ class CommentWOutFK
33
+ include MongoMapper::Document
34
+ key :description, String
35
+
36
+ belongs_to :post
37
+ end
38
+
39
+ class EmbeddedCommentNoSpecification
40
+ include MongoMapper::EmbeddedDocument
41
+ end
42
+
43
+ describe Mongoa::MongoMapper::Matchers do
44
+ describe "#belongs_to" do
45
+ describe "document" do
46
+ it "should return true of the belongs_to relationship is specified and the foreign key is present" do
47
+ matcher = Mongoa::MongoMapper::MongoAssociationMatcher.new(:belongs_to, :post)
48
+ matcher.should be_matches(Comment.new)
49
+ end
50
+
51
+ it "should return false if the belongs_to is present but the foreign key is missing" do
52
+ matcher = Mongoa::MongoMapper::MongoAssociationMatcher.new(:belongs_to, :post)
53
+ matcher.should_not be_matches(CommentWOutFK.new)
54
+ end
55
+ end
56
+
57
+ describe "embedded" do
58
+ it "should return true if the belongs_to relationship is specified" do
59
+ matcher = Mongoa::MongoMapper::MongoAssociationMatcher.new(:belongs_to, :post)
60
+ matcher.should be_matches(EmbeddedComment.new)
61
+ end
62
+
63
+ it "should return false if the belongs_to relationship is not specified" do
64
+ matcher = Mongoa::MongoMapper::MongoAssociationMatcher.new(:belongs_to, :post)
65
+ matcher.should_not be_matches(EmbeddedCommentNoSpecification.new)
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "#has_many" do
71
+ it "should return true if the has_many relationship is specified" do
72
+ matcher = Mongoa::MongoMapper::MongoAssociationMatcher.new(:has_many, :comments)
73
+ matcher.should be_matches(Post.new)
74
+ end
75
+
76
+ it "should return false of the has_many relationship is not specified" do
77
+ matcher = Mongoa::MongoMapper::MongoAssociationMatcher.new(:has_many, :comments)
78
+ matcher.should_not be_matches(PostWOutHM.new)
79
+ end
80
+ end
81
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,23 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'mongoa'
4
- require 'spec'
5
- require 'spec/autorun'
1
+ # $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ # $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'mongo_mapper'
5
+ require 'mongo_mapper/plugins'
6
+ require 'mongo_mapper/plugins/associations'
7
+
8
+ config = {
9
+ 'test' => {'host' => 'localhost', 'port' => 27017, 'database' => 'mongoa_test'},
10
+ }
11
+
12
+ MongoMapper.config = config
13
+ MongoMapper.connect("test")
14
+
15
+ require 'rspec'
6
16
 
7
- Spec::Runner.configure do |config|
8
-
17
+ Rspec.configure do |config|
18
+
9
19
  end
20
+
21
+ require 'mongoa'
22
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/mongoa/mongo_mapper/association_matcher')
23
+
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Post
4
+ include MongoMapper::Document
5
+
6
+ key :name, String
7
+
8
+ validates_presence_of :name
9
+ end
10
+
11
+ class PostRequired
12
+ include MongoMapper::Document
13
+
14
+ key :name, String, :required => true
15
+ end
16
+
17
+
18
+ describe Mongoa::MongoMapper::Matchers do
19
+ describe "#validates_presence_of" do
20
+ it "should return true if the key has a validates_presence_of validtion" do
21
+ validation = Mongoa::MongoMapper::ValidateMatcher.new("ValidatesPresenceOf", :name)
22
+ validation.should be_matches(Post.new)
23
+ end
24
+
25
+ it "should return false if the key does not have a validates_presence_of validtion" do
26
+ validation = Mongoa::MongoMapper::ValidateMatcher.new("ValidatesPresenceOf", :foo)
27
+ validation.should_not be_matches(Post.new)
28
+ end
29
+ end
30
+
31
+ describe "required" do
32
+ it "should work the same if the key is required rather than validates_presence_of" do
33
+ validation = Mongoa::MongoMapper::ValidateMatcher.new("ValidatesPresenceOf", :name)
34
+ validation.should be_matches(PostRequired.new)
35
+
36
+ validation = Mongoa::MongoMapper::ValidateMatcher.new("ValidatesPresenceOf", :foo)
37
+ validation.should_not be_matches(PostRequired.new)
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Scott J. Tamosunas
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-09 00:00:00 -04:00
17
+ date: 2010-08-11 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -51,13 +51,14 @@ files:
51
51
  - lib/mongoa.rb
52
52
  - lib/mongoa/integrations/rspec2.rb
53
53
  - lib/mongoa/mongo_mapper/association_matcher.rb
54
- - lib/mongoa/mongo_mapper/associations.rb
54
+ - lib/mongoa/mongo_mapper/improvements/mongo_mapper_associations.rb
55
55
  - lib/mongoa/mongo_mapper/matchers.rb
56
56
  - lib/mongoa/mongo_mapper/validations.rb
57
57
  - mongoa.gemspec
58
- - spec/mongoa_spec.rb
58
+ - spec/association_matcher_spec.rb
59
59
  - spec/spec.opts
60
60
  - spec/spec_helper.rb
61
+ - spec/validation_matcher_spec.rb
61
62
  has_rdoc: true
62
63
  homepage: http://github.com/scotttam/mongoa
63
64
  licenses: []
@@ -91,5 +92,6 @@ signing_key:
91
92
  specification_version: 3
92
93
  summary: Like Shoulda but for MongoMapper.
93
94
  test_files:
94
- - spec/mongoa_spec.rb
95
+ - spec/association_matcher_spec.rb
95
96
  - spec/spec_helper.rb
97
+ - spec/validation_matcher_spec.rb
data/spec/mongoa_spec.rb DELETED
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Mongoa" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end