mongoa 0.1.17 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.17
1
+ 0.2.1
@@ -82,7 +82,8 @@ module Mongoa
82
82
  end
83
83
 
84
84
  def belongs_to_foreign_key_exists?
85
- model_class.keys.keys.include?("#{reflection_name}_id")
85
+ foreign_key = model_class.keys["#{reflection_name}_id"]
86
+ foreign_key && foreign_key.type
86
87
  end
87
88
 
88
89
  def reflection_name
@@ -4,6 +4,8 @@ require 'mongoa/mongo_mapper/validations/validate_presence_of'
4
4
  require 'mongoa/mongo_mapper/validations/validate_inclusion_of'
5
5
  require 'mongoa/mongo_mapper/validations/validate_uniqueness_of'
6
6
  require 'mongoa/mongo_mapper/validations/validate_length_of'
7
+ require 'mongoa/mongo_mapper/validations/validate_format_of'
8
+ require 'mongoa/mongo_mapper/validations/validate_numericality_of'
7
9
 
8
10
  module Mongoa
9
11
  module MongoMapper
@@ -0,0 +1,41 @@
1
+ module Mongoa
2
+ module MongoMapper
3
+ module Matchers
4
+ class ValidateFormatOfMatcher < ValidateBase
5
+ def initialize(attribute)
6
+ super(attribute)
7
+ end
8
+
9
+ def matches?(subject)
10
+ super(subject)
11
+ raise "The :with is a required option to be checked, but you didn't check it." if !@with
12
+ @validation.with == @with if @validation && @validation.with
13
+ end
14
+
15
+ def with(value)
16
+ raise "Can't use both with and not with" if @not_with
17
+ @with = value
18
+ self
19
+ end
20
+
21
+ def description
22
+ "require #{@attribute} to validate the format of"
23
+ end
24
+
25
+ def failure_message
26
+ "Expected #{@attribute} to validate the format of but didn't"
27
+ end
28
+
29
+ def negative_failure_message
30
+ "Expected #{@attribute} not to validate the format but did"
31
+ end
32
+
33
+ private
34
+
35
+ def validation_type
36
+ "ValidatesFormatOf"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,33 @@
1
+ module Mongoa
2
+ module MongoMapper
3
+ module Matchers
4
+ class ValidateNumericalityOfMatcher < ValidateBase
5
+ def initialize(attribute)
6
+ super(attribute)
7
+ end
8
+
9
+ def matches?(subject)
10
+ super(subject)
11
+ end
12
+
13
+ def description
14
+ "require #{@attribute} to validate the numericality of"
15
+ end
16
+
17
+ def failure_message
18
+ "Expected #{@attribute} to validate the numericality of but didn't"
19
+ end
20
+
21
+ def negative_failure_message
22
+ "Expected #{@attribute} not to validate the numericality but did"
23
+ end
24
+
25
+ private
26
+
27
+ def validation_type
28
+ "ValidatesNumericalityOf"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ 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.17"
8
+ s.version = "0.2.1"
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-27}
12
+ s.date = %q{2010-10-20}
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 = [
@@ -28,16 +28,20 @@ Gem::Specification.new do |s|
28
28
  "lib/mongoa/mongo_mapper/associations/all.rb",
29
29
  "lib/mongoa/mongo_mapper/matchers.rb",
30
30
  "lib/mongoa/mongo_mapper/validations/validate_base.rb",
31
+ "lib/mongoa/mongo_mapper/validations/validate_format_of.rb",
31
32
  "lib/mongoa/mongo_mapper/validations/validate_inclusion_of.rb",
32
33
  "lib/mongoa/mongo_mapper/validations/validate_length_of.rb",
34
+ "lib/mongoa/mongo_mapper/validations/validate_numericality_of.rb",
33
35
  "lib/mongoa/mongo_mapper/validations/validate_presence_of.rb",
34
36
  "lib/mongoa/mongo_mapper/validations/validate_uniqueness_of.rb",
35
37
  "mongoa.gemspec",
36
38
  "spec/assoications/all_spec.rb",
37
39
  "spec/matchers_spec.rb",
38
40
  "spec/spec_helper.rb",
41
+ "spec/validations/validate_format_of_spec.rb",
39
42
  "spec/validations/validate_inclusion_of_spec.rb",
40
43
  "spec/validations/validate_length_of_spec.rb",
44
+ "spec/validations/validate_numercality_of_spec.rb",
41
45
  "spec/validations/validate_presence_of_spec.rb",
42
46
  "spec/validations/validate_uniqueness_of_spec.rb"
43
47
  ]
@@ -50,8 +54,11 @@ Gem::Specification.new do |s|
50
54
  "spec/assoications/all_spec.rb",
51
55
  "spec/matchers_spec.rb",
52
56
  "spec/spec_helper.rb",
57
+ "spec/validations/validate_exclusion_of_spec.rb",
58
+ "spec/validations/validate_format_of_spec.rb",
53
59
  "spec/validations/validate_inclusion_of_spec.rb",
54
60
  "spec/validations/validate_length_of_spec.rb",
61
+ "spec/validations/validate_numercality_of_spec.rb",
55
62
  "spec/validations/validate_presence_of_spec.rb",
56
63
  "spec/validations/validate_uniqueness_of_spec.rb"
57
64
  ]
@@ -5,7 +5,7 @@ class Post
5
5
  key :name, String
6
6
 
7
7
  has_many :comments
8
- has_many :commentwoutfks
8
+ has_many :comment_w_out_fks
9
9
  has_many :embedded_comment_no_specifications
10
10
  end
11
11
 
@@ -29,7 +29,14 @@ class EmbeddedComment
29
29
  belongs_to :post
30
30
  end
31
31
 
32
- class CommentWOutFK
32
+ class CommentWOutFk
33
+ include MongoMapper::Document
34
+ key :description, String
35
+
36
+ belongs_to :post
37
+ end
38
+
39
+ class CommentWOutFKAddedDynamicallyOnCreate
33
40
  include MongoMapper::Document
34
41
  key :description, String
35
42
 
@@ -70,7 +77,14 @@ describe Mongoa::MongoMapper do
70
77
 
71
78
  it "should return false if the belongs_to is present but the foreign key is missing" do
72
79
  matcher = Mongoa::MongoMapper::MongoAssociationMatcher.new(:belongs_to, :post)
73
- matcher.should_not be_matches(CommentWOutFK.new)
80
+ matcher.should_not be_matches(CommentWOutFk.new)
81
+ end
82
+
83
+ it "should return false if the belongs_to is present but the foreign key exists and it's type is null (indicating that the key was added through a create dynamically and not defined on the class)" do
84
+ post = Post.create(:name => "my post")
85
+ CommentWOutFKAddedDynamicallyOnCreate.create(:description => "hello", :post => post)
86
+ matcher = Mongoa::MongoMapper::MongoAssociationMatcher.new(:belongs_to, :post)
87
+ matcher.should_not be_matches(CommentWOutFKAddedDynamicallyOnCreate.new)
74
88
  end
75
89
  end
76
90
 
data/spec/spec_helper.rb CHANGED
@@ -20,6 +20,19 @@ end
20
20
  require 'mongoa'
21
21
  require File.expand_path(File.dirname(__FILE__) + '/../lib/mongoa/mongo_mapper/matchers')
22
22
 
23
+ class User
24
+ include MongoMapper::Document
25
+
26
+ key :name, String
27
+ key :email, String
28
+ key :years_alive, Integer
29
+ key :years_alive_numeric, Integer, :numeric => true
30
+ key :email_format, String, :format => /(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i
31
+
32
+ validates_format_of :email, :with => /(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i
33
+ validates_numericality_of :years_alive
34
+ end
35
+
23
36
  class Post
24
37
  include MongoMapper::Document
25
38
 
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Mongoa::MongoMapper do
4
+ describe "validates_exclusion_of or key not_in => :true" do
5
+ describe "#validates_exclusion_of" do
6
+ context "in" do
7
+ it "should return true if the key has a validates_exclusion_of validtion and it's value is not in the list" do
8
+ validation = Mongoa::MongoMapper::Matchers::ValidateExclusionOfMatcher.new(:name)
9
+ validation.in(%w(admin user)).should be_matches(Post.new)
10
+ end
11
+
12
+ it "should raise an exception if the in is not specified"
13
+ # it "should return false if the key does not have a validates_presence_of validtion" do
14
+ # validation = Mongoa::MongoMapper::Matchers::ValidateExclusionOfMatcher.new(:foo)
15
+ # validation.should_not be_matches(Post.new)
16
+ # end
17
+ end
18
+ end
19
+ end
20
+
21
+ describe "not_in" do
22
+ # it "should work the same if the key is required rather than validates_presence_of" do
23
+ # validation = Mongoa::MongoMapper::Matchers::ValidateExclusionOfMatcher.new(:name)
24
+ # validation.should be_matches(PostRequired.new)
25
+ #
26
+ # validation = Mongoa::MongoMapper::Matchers::ValidateExclusionOfMatcher.new(:foo)
27
+ # validation.should_not be_matches(PostRequired.new)
28
+ # end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Mongoa::MongoMapper do
4
+ describe "validates_format_of or key format => <whatev>" do
5
+ describe "#validates_format_of" do
6
+ context "with" do
7
+ before(:each) do
8
+ @validation = Mongoa::MongoMapper::Matchers::ValidateFormatOfMatcher.new(:email)
9
+ end
10
+ it "should return true if the key has a validates_format_of validtion" do
11
+ @validation.with(/(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i).should be_matches(User.new)
12
+ end
13
+
14
+ it "should return false if the with doesn't match" do
15
+ @validation.with(/blah/i).should_not be_matches(User.new)
16
+ end
17
+
18
+ it "should raise an exception if the required option :with is not specified" do
19
+ lambda { @validation.matches?(User.new) }.should raise_error(RuntimeError)
20
+ end
21
+ end
22
+
23
+ it "should return false if the key does not have a validates_format_of validtion" do
24
+ validation = Mongoa::MongoMapper::Matchers::ValidateFormatOfMatcher.new(:name)
25
+ validation.with(/(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i).should_not be_matches(User.new)
26
+ end
27
+ end
28
+
29
+ describe "format" do
30
+ it "should work the same if the key is format rather than validates_format_of" do
31
+ validation = Mongoa::MongoMapper::Matchers::ValidateFormatOfMatcher.new(:email_format)
32
+ validation.with(/(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i).should be_matches(User.new)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Mongoa::MongoMapper do
4
+ describe "validates_numericality_of or key numeric" do
5
+ describe "#validates_numericality_of" do
6
+ it "should return true if the key has a validates_numericality_of validtion" do
7
+ validation = Mongoa::MongoMapper::Matchers::ValidateNumericalityOfMatcher.new(:years_alive)
8
+ validation.should be_matches(User.new)
9
+ end
10
+
11
+ it "should return false if it doesn't have a validates_numericality_of" do
12
+ validation = Mongoa::MongoMapper::Matchers::ValidateNumericalityOfMatcher.new(:foo)
13
+ validation.should_not be_matches(User.new)
14
+ end
15
+ end
16
+
17
+ describe "numeric" do
18
+ it "should work the same if the key is numeric rather than validates_numericality_of" do
19
+ validation = Mongoa::MongoMapper::Matchers::ValidateNumericalityOfMatcher.new(:years_alive_numeric)
20
+ validation.should be_matches(User.new)
21
+ end
22
+ end
23
+ end
24
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 2
7
8
  - 1
8
- - 17
9
- version: 0.1.17
9
+ version: 0.2.1
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-27 00:00:00 -04:00
17
+ date: 2010-10-20 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -53,18 +53,23 @@ files:
53
53
  - lib/mongoa/mongo_mapper/associations/all.rb
54
54
  - lib/mongoa/mongo_mapper/matchers.rb
55
55
  - lib/mongoa/mongo_mapper/validations/validate_base.rb
56
+ - lib/mongoa/mongo_mapper/validations/validate_format_of.rb
56
57
  - lib/mongoa/mongo_mapper/validations/validate_inclusion_of.rb
57
58
  - lib/mongoa/mongo_mapper/validations/validate_length_of.rb
59
+ - lib/mongoa/mongo_mapper/validations/validate_numericality_of.rb
58
60
  - lib/mongoa/mongo_mapper/validations/validate_presence_of.rb
59
61
  - lib/mongoa/mongo_mapper/validations/validate_uniqueness_of.rb
60
62
  - mongoa.gemspec
61
63
  - spec/assoications/all_spec.rb
62
64
  - spec/matchers_spec.rb
63
65
  - spec/spec_helper.rb
66
+ - spec/validations/validate_format_of_spec.rb
64
67
  - spec/validations/validate_inclusion_of_spec.rb
65
68
  - spec/validations/validate_length_of_spec.rb
69
+ - spec/validations/validate_numercality_of_spec.rb
66
70
  - spec/validations/validate_presence_of_spec.rb
67
71
  - spec/validations/validate_uniqueness_of_spec.rb
72
+ - spec/validations/validate_exclusion_of_spec.rb
68
73
  has_rdoc: true
69
74
  homepage: http://github.com/scotttam/mongoa
70
75
  licenses: []
@@ -101,7 +106,10 @@ test_files:
101
106
  - spec/assoications/all_spec.rb
102
107
  - spec/matchers_spec.rb
103
108
  - spec/spec_helper.rb
109
+ - spec/validations/validate_exclusion_of_spec.rb
110
+ - spec/validations/validate_format_of_spec.rb
104
111
  - spec/validations/validate_inclusion_of_spec.rb
105
112
  - spec/validations/validate_length_of_spec.rb
113
+ - spec/validations/validate_numercality_of_spec.rb
106
114
  - spec/validations/validate_presence_of_spec.rb
107
115
  - spec/validations/validate_uniqueness_of_spec.rb