mongoa 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoa/mongo_mapper/validations/validate_base.rb +34 -0
- data/lib/mongoa/mongo_mapper/validations/validate_inclusion_of.rb +35 -0
- data/lib/mongoa/mongo_mapper/validations/validate_presence_of.rb +25 -0
- data/lib/mongoa/mongo_mapper/validations.rb +7 -34
- data/mongoa.gemspec +5 -2
- data/spec/validation_matcher_spec.rb +60 -13
- metadata +6 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.7
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Mongoa
|
2
|
+
module MongoMapper
|
3
|
+
module Matchers
|
4
|
+
class ValidateBase
|
5
|
+
attr_reader :attribute
|
6
|
+
|
7
|
+
def initialize(attribute)
|
8
|
+
@attribute = attribute
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(subject)
|
12
|
+
@subject = subject
|
13
|
+
@validation = find_validation
|
14
|
+
end
|
15
|
+
|
16
|
+
def validation_type
|
17
|
+
raise "Redefine in the subclass"
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def model_class
|
23
|
+
@subject.class
|
24
|
+
end
|
25
|
+
|
26
|
+
def find_validation
|
27
|
+
attr_validations = model_class.validations.select { |validation| validation.attribute == attribute }
|
28
|
+
return nil unless attr_validations
|
29
|
+
validation = attr_validations.detect { |validation| validation.key.include?(validation_type) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Mongoa
|
2
|
+
module MongoMapper
|
3
|
+
module Matchers
|
4
|
+
class ValidateInclusionOfMatcher < ValidateBase
|
5
|
+
def initialize(attribute, within)
|
6
|
+
super(attribute)
|
7
|
+
@within = within
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(subject)
|
11
|
+
super(subject)
|
12
|
+
@validation ? @validation.within == @within : false
|
13
|
+
end
|
14
|
+
|
15
|
+
def description
|
16
|
+
"require #{@attribute} to be within #{@within}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def failure_message
|
20
|
+
"Expected #{@attribute} to be within #{@within} but was not"
|
21
|
+
end
|
22
|
+
|
23
|
+
def negative_failure_message
|
24
|
+
"Expected #{@attribute} to not be within #{@within} but was"
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def validation_type
|
30
|
+
"ValidatesInclusionOf"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mongoa
|
2
|
+
module MongoMapper
|
3
|
+
module Matchers
|
4
|
+
class ValidatePresenceOfMatcher < ValidateBase
|
5
|
+
def description
|
6
|
+
"require #{@attribute} to be set"
|
7
|
+
end
|
8
|
+
|
9
|
+
def failure_message
|
10
|
+
"Expected #{@attribute} to be a required field (validates_presence_of or :required => true) but was not"
|
11
|
+
end
|
12
|
+
|
13
|
+
def negative_failure_message
|
14
|
+
"Expected #{@attribute} to not be a required field (validates_presence_of or :required => true), but it was"
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def validation_type
|
20
|
+
"ValidatesPresenceOf"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,43 +1,16 @@
|
|
1
|
+
require 'mongoa/mongo_mapper/validations/validate_base'
|
2
|
+
require 'mongoa/mongo_mapper/validations/validate_presence_of'
|
3
|
+
require 'mongoa/mongo_mapper/validations/validate_inclusion_of'
|
4
|
+
|
1
5
|
module Mongoa
|
2
6
|
module MongoMapper
|
3
7
|
module Matchers
|
4
8
|
def validate_presence_of(attr)
|
5
|
-
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
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
|
17
|
-
|
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
|
24
|
-
|
25
|
-
def description
|
26
|
-
"require #{@attribute} to be set"
|
27
|
-
end
|
28
|
-
|
29
|
-
def failure_message
|
30
|
-
"#{@attribute} to be a required field (validates_presence_of) but was not"
|
31
|
-
end
|
32
|
-
|
33
|
-
def negative_failure_message
|
34
|
-
"#{@attribute} to not be a required field (validates_presence_of), but it was"
|
9
|
+
ValidatePresenceOfMatcher.new(attr)
|
35
10
|
end
|
36
11
|
|
37
|
-
|
38
|
-
|
39
|
-
def model_class
|
40
|
-
@subject.class
|
12
|
+
def validates_inclusion_of(attr, within)
|
13
|
+
ValidateInclusionOfMatcher.new(attr, within)
|
41
14
|
end
|
42
15
|
end
|
43
16
|
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.
|
8
|
+
s.version = "0.1.7"
|
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-
|
12
|
+
s.date = %q{2010-08-12}
|
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 = [
|
@@ -29,6 +29,9 @@ Gem::Specification.new do |s|
|
|
29
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
|
+
"lib/mongoa/mongo_mapper/validations/validate_base.rb",
|
33
|
+
"lib/mongoa/mongo_mapper/validations/validate_inclusion_of.rb",
|
34
|
+
"lib/mongoa/mongo_mapper/validations/validate_presence_of.rb",
|
32
35
|
"mongoa.gemspec",
|
33
36
|
"spec/association_matcher_spec.rb",
|
34
37
|
"spec/spec.opts",
|
@@ -14,27 +14,74 @@ class PostRequired
|
|
14
14
|
key :name, String, :required => true
|
15
15
|
end
|
16
16
|
|
17
|
+
class Within
|
18
|
+
include MongoMapper::Document
|
19
|
+
|
20
|
+
key :state, String
|
21
|
+
|
22
|
+
validates_inclusion_of :state, :within => ["new", "uploaded"]
|
23
|
+
end
|
24
|
+
|
25
|
+
class WithinIn
|
26
|
+
include MongoMapper::Document
|
27
|
+
|
28
|
+
key :state, String, :in => ["new", "uploaded"]
|
29
|
+
end
|
17
30
|
|
18
31
|
describe Mongoa::MongoMapper::Matchers do
|
19
|
-
describe "
|
20
|
-
|
21
|
-
|
22
|
-
|
32
|
+
describe "validates_presence_of or key required => :true" do
|
33
|
+
describe "#validates_presence_of" do
|
34
|
+
it "should return true if the key has a validates_presence_of validtion" do
|
35
|
+
validation = Mongoa::MongoMapper::Matchers::ValidatePresenceOfMatcher.new(:name)
|
36
|
+
validation.should be_matches(Post.new)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should return false if the key does not have a validates_presence_of validtion" do
|
40
|
+
validation = Mongoa::MongoMapper::Matchers::ValidatePresenceOfMatcher.new(:foo)
|
41
|
+
validation.should_not be_matches(Post.new)
|
42
|
+
end
|
23
43
|
end
|
44
|
+
|
45
|
+
describe "required" do
|
46
|
+
it "should work the same if the key is required rather than validates_presence_of" do
|
47
|
+
validation = Mongoa::MongoMapper::Matchers::ValidatePresenceOfMatcher.new(:name)
|
48
|
+
validation.should be_matches(PostRequired.new)
|
24
49
|
|
25
|
-
|
26
|
-
|
27
|
-
|
50
|
+
validation = Mongoa::MongoMapper::Matchers::ValidatePresenceOfMatcher.new(:foo)
|
51
|
+
validation.should_not be_matches(PostRequired.new)
|
52
|
+
end
|
28
53
|
end
|
29
54
|
end
|
30
55
|
|
31
|
-
describe "
|
32
|
-
|
33
|
-
|
34
|
-
|
56
|
+
describe "#validates_inclusion_of or key :in => []" do
|
57
|
+
describe "validates_inclusion_of" do
|
58
|
+
it "should return true if the key has a validate_inclusion_of with the specified within" do
|
59
|
+
validation = Mongoa::MongoMapper::Matchers::ValidateInclusionOfMatcher.new(:state, ["new", "uploaded"])
|
60
|
+
validation.should be_matches(Within.new)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return false if the key has a validate_inclusion_of but the within doesn't match" do
|
64
|
+
validation = Mongoa::MongoMapper::Matchers::ValidateInclusionOfMatcher.new(:state, ["goo", "gaa"])
|
65
|
+
validation.should_not be_matches(Within.new)
|
66
|
+
end
|
35
67
|
|
36
|
-
|
37
|
-
|
68
|
+
it "should return false if the key does not have a validate_inclusion_of set" do
|
69
|
+
validation = Mongoa::MongoMapper::Matchers::ValidateInclusionOfMatcher.new(:foo, ["new", "uploaded"])
|
70
|
+
validation.should_not be_matches(Within.new)
|
71
|
+
end
|
38
72
|
end
|
73
|
+
|
74
|
+
describe "in" do
|
75
|
+
it "should work the same as validates_inclusion_of" do
|
76
|
+
validation = Mongoa::MongoMapper::Matchers::ValidateInclusionOfMatcher.new(:state, ["new", "uploaded"])
|
77
|
+
validation.should be_matches(WithinIn.new)
|
78
|
+
|
79
|
+
validation = Mongoa::MongoMapper::Matchers::ValidateInclusionOfMatcher.new(:state, ["goo", "gaa"])
|
80
|
+
validation.should_not be_matches(WithinIn.new)
|
81
|
+
|
82
|
+
validation = Mongoa::MongoMapper::Matchers::ValidateInclusionOfMatcher.new(:foo, ["new", "uploaded"])
|
83
|
+
validation.should_not be_matches(WithinIn.new)
|
84
|
+
end
|
85
|
+
end
|
39
86
|
end
|
40
87
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 7
|
9
|
+
version: 0.1.7
|
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-
|
17
|
+
date: 2010-08-12 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -54,6 +54,9 @@ files:
|
|
54
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
|
+
- lib/mongoa/mongo_mapper/validations/validate_base.rb
|
58
|
+
- lib/mongoa/mongo_mapper/validations/validate_inclusion_of.rb
|
59
|
+
- lib/mongoa/mongo_mapper/validations/validate_presence_of.rb
|
57
60
|
- mongoa.gemspec
|
58
61
|
- spec/association_matcher_spec.rb
|
59
62
|
- spec/spec.opts
|