mongoa 0.1.8 → 0.1.9

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.8
1
+ 0.1.9
@@ -0,0 +1,65 @@
1
+ module Mongoa
2
+ module MongoMapper
3
+ module Matchers
4
+ class ValidateLengthOfMatcher < ValidateBase
5
+ def initialize(attribute, length_options)
6
+ super(attribute)
7
+
8
+ if length_options[:length]
9
+ @length_options = case length_options[:length]
10
+ when Integer
11
+ { :minimum => 0, :maximum => length_options[:length] }
12
+ when Range
13
+ { :within => length_options[:length] }
14
+ when Hash
15
+ length_options[:length]
16
+ end
17
+ else
18
+ @length_options = length_options
19
+ end
20
+ end
21
+
22
+ def matches?(subject)
23
+ super(subject)
24
+ if @validation
25
+ if @length_options.keys.include?(:minimum)
26
+ result = @validation.minimum == @length_options[:minimum]
27
+ return false if !result
28
+ end
29
+
30
+ if @length_options.keys.include?(:maximum)
31
+ result = @validation.maximum == @length_options[:maximum]
32
+ return false if !result
33
+ end
34
+
35
+ if @length_options.keys.include?(:within)
36
+ result = @validation.within == @length_options[:within]
37
+ return false if !result
38
+ end
39
+ true
40
+ else
41
+ return false
42
+ end
43
+ end
44
+
45
+ def description
46
+ "require #{@attribute} to be a maximum length of #{@maximum}"
47
+ end
48
+
49
+ def failure_message
50
+ "Expected #{@attribute} to be a maximum length of #{@maximum}"
51
+ end
52
+
53
+ def negative_failure_message
54
+ "Expected #{@attribute} to not be a maximum length of #{@maximum}"
55
+ end
56
+
57
+ private
58
+
59
+ def validation_type
60
+ "ValidatesLengthOf"
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,6 +1,7 @@
1
1
  require 'mongoa/mongo_mapper/validations/validate_base'
2
2
  require 'mongoa/mongo_mapper/validations/validate_presence_of'
3
3
  require 'mongoa/mongo_mapper/validations/validate_inclusion_of'
4
+ require 'mongoa/mongo_mapper/validations/validate_length_of'
4
5
 
5
6
  module Mongoa
6
7
  module MongoMapper
data/mongoa.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoa}
8
- s.version = "0.1.8"
8
+ s.version = "0.1.9"
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"]
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/mongoa/mongo_mapper/validations.rb",
32
32
  "lib/mongoa/mongo_mapper/validations/validate_base.rb",
33
33
  "lib/mongoa/mongo_mapper/validations/validate_inclusion_of.rb",
34
+ "lib/mongoa/mongo_mapper/validations/validate_length_of.rb",
34
35
  "lib/mongoa/mongo_mapper/validations/validate_presence_of.rb",
35
36
  "mongoa.gemspec",
36
37
  "spec/association_matcher_spec.rb",
@@ -6,12 +6,14 @@ class Post
6
6
  key :name, String
7
7
 
8
8
  validates_presence_of :name
9
+ validates_length_of :name, :minimum => 4, :maximum => 32
9
10
  end
10
11
 
11
12
  class PostRequired
12
13
  include MongoMapper::Document
13
14
 
14
- key :name, String, :required => true
15
+ key :name, String, :required => true, :length => 32
16
+ key :range_name, String, :required => true, :length => 0..56
15
17
  end
16
18
 
17
19
  class Within
@@ -84,4 +86,58 @@ describe Mongoa::MongoMapper::Matchers do
84
86
  end
85
87
  end
86
88
  end
89
+
90
+ describe "#validates_length_of or key :length => <length_options>" do
91
+ describe "validates_length_of" do
92
+ describe "maximum" do
93
+ it "should return true if the key has a validates_length of with the specified :maximum" do
94
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:name, :maximum => 32)
95
+ validation.should be_matches(Post.new)
96
+ end
97
+
98
+ it "should return false if the key has a validates_length but the maximum's don't match" do
99
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:name, :maximum => 25)
100
+ validation.should_not be_matches(Post.new)
101
+ end
102
+
103
+ it "should return false if the key does not have a validates_length of" do
104
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:foo, :maximum => 32)
105
+ validation.should_not be_matches(Post.new)
106
+ end
107
+ end
108
+
109
+ describe "minimum" do
110
+ it "should return true if the key has a validates_length of with the specified :minimum" do
111
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:name, :minimum => 4)
112
+ validation.should be_matches(Post.new)
113
+ end
114
+
115
+ it "should return false if the key has a validates_length of but the minimum's don't match" do
116
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:name, :minimum => 2)
117
+ validation.should_not be_matches(Post.new)
118
+ end
119
+
120
+ it "should return false if the key does not have a validates_length" do
121
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:foo, :minimum => 32)
122
+ validation.should_not be_matches(Post.new)
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "length" do
128
+ it "should work the same as validates_length_of" do
129
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:name, :length => 32)
130
+ validation.should be_matches(PostRequired.new)
131
+
132
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:name, :length => 25)
133
+ validation.should_not be_matches(PostRequired.new)
134
+
135
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:foo, :length => 32)
136
+ validation.should_not be_matches(PostRequired.new)
137
+
138
+ validation = Mongoa::MongoMapper::Matchers::ValidateLengthOfMatcher.new(:range_name, :length => 0..56)
139
+ validation.should be_matches(PostRequired.new)
140
+ end
141
+ end
142
+ end
87
143
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 8
9
- version: 0.1.8
8
+ - 9
9
+ version: 0.1.9
10
10
  platform: ruby
11
11
  authors:
12
12
  - Scott J. Tamosunas
@@ -56,6 +56,7 @@ files:
56
56
  - lib/mongoa/mongo_mapper/validations.rb
57
57
  - lib/mongoa/mongo_mapper/validations/validate_base.rb
58
58
  - lib/mongoa/mongo_mapper/validations/validate_inclusion_of.rb
59
+ - lib/mongoa/mongo_mapper/validations/validate_length_of.rb
59
60
  - lib/mongoa/mongo_mapper/validations/validate_presence_of.rb
60
61
  - mongoa.gemspec
61
62
  - spec/association_matcher_spec.rb