whyvalidationssuckin96 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.3
1
+ 1.3.0
@@ -0,0 +1,16 @@
1
+ module WhyValidationsSuckIn96
2
+ class ValidationCollection < Array
3
+
4
+ def to_json(options={})
5
+ inject({}) do |acc,validation|
6
+ acc[validation.attribute] = validation.options
7
+ acc
8
+ end.to_json(options)
9
+ end
10
+
11
+ def select(&block)
12
+ self.class.new super
13
+ end
14
+
15
+ end
16
+ end
@@ -1,4 +1,5 @@
1
1
  require 'whyvalidationssuckin96/validation_builder'
2
+ require 'whyvalidationssuckin96/validation_collection'
2
3
 
3
4
  module WhyValidationsSuckIn96
4
5
  module ValidationSupport
@@ -43,8 +44,9 @@ module WhyValidationsSuckIn96
43
44
  # An array of instances of all validations for this object
44
45
  # @return [Array]
45
46
  def all_validations
46
- @all_validations ||= self.class.validation_collection.collect do |(vc,opts)|
47
- vc.new(self, opts)
47
+ @all_validations ||= self.class.validation_collection.inject(ValidationCollection.new) do |vc, (v,opts)|
48
+ vc << v.new(self, opts)
49
+ vc
48
50
  end
49
51
  end
50
52
 
@@ -72,4 +74,4 @@ module WhyValidationsSuckIn96
72
74
 
73
75
  end # ClassMethods
74
76
  end # ValidationSupport
75
- end # WhyValidationsSuckIn96
77
+ end # WhyValidationsSuckIn96
@@ -0,0 +1,63 @@
1
+ require 'teststrap'
2
+ require 'active_support'
3
+
4
+ context "validation_collection" do
5
+ context "to_json" do
6
+ setup do
7
+ @validation_options = { :is => 3 }
8
+ @collection = WhyValidationsSuckIn96::ValidationCollection.new
9
+ @collection << WhyValidationsSuckIn96::ValidatesLength.new(Object.new, { :attribute => :thing }.merge(@validation_options))
10
+ ActiveSupport::JSON.decode(@collection.to_json)
11
+ end
12
+
13
+ should "have keys for each attribute that is validated" do
14
+ topic.keys
15
+ end.equals(['thing'])
16
+
17
+ should "not have a value that is the attribute name" do
18
+ !topic['thing'].keys.include? 'thing'
19
+ end
20
+
21
+ should "have a message value that is the validation's message" do
22
+ topic['thing']['message'] == @collection.first.message
23
+ end
24
+
25
+ should "have values of the validation options" do
26
+ @validation_options.all? {|option,value| topic['thing'][option.to_s] == value }
27
+ end
28
+
29
+ context "provided a hash of options" do
30
+ setup do
31
+ @collection << WhyValidationsSuckIn96::ValidatesNumericality.new(Object.new, { :attribute => :other_thing })
32
+ ActiveSupport::JSON.decode(@collection.to_json(:only => :thing))
33
+ end
34
+
35
+ should "respect those options when generating json" do
36
+ topic.keys == ['thing']
37
+ end
38
+ end
39
+
40
+ context "selecting a subset" do
41
+ setup do
42
+ @collection << WhyValidationsSuckIn96::ValidatesNumericality.new(Object.new, { :attribute => :other_thing })
43
+ @collection.select { |v| v.attribute == :thing }
44
+ end
45
+
46
+ should "return a ValidationCollection" do
47
+ topic.instance_of? WhyValidationsSuckIn96::ValidationCollection
48
+ end
49
+ end
50
+
51
+ context "rejecting a subset" do
52
+ setup do
53
+ @collection << WhyValidationsSuckIn96::ValidatesNumericality.new(Object.new, { :attribute => :other_thing })
54
+ @collection.reject { |v| v.attribute == :thing }
55
+ end
56
+
57
+ should "return a ValidationCollection" do
58
+ topic.instance_of? WhyValidationsSuckIn96::ValidationCollection
59
+ end
60
+ end
61
+
62
+ end
63
+ end
@@ -34,6 +34,10 @@ context "validation_support" do
34
34
  topic.new
35
35
  end.respond_to(:all_validations)
36
36
 
37
+ should "have a ValidationCollection for all_validations" do
38
+ topic.new.all_validations.instance_of?(WhyValidationsSuckIn96::ValidationCollection)
39
+ end
40
+
37
41
  should "have an empty validation collection" do
38
42
  topic.validation_collection.size
39
43
  end.equals(0)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{whyvalidationssuckin96}
8
- s.version = "1.2.3"
8
+ s.version = "1.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["gabrielg", "douglasmeyer"]
12
- s.date = %q{2009-12-22}
12
+ s.date = %q{2010-01-15}
13
13
  s.description = %q{A library for setting up model validations, such as in ActiveRecord.}
14
14
  s.email = %q{gabriel.gironda@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -91,6 +91,7 @@ Gem::Specification.new do |s|
91
91
  "lib/whyvalidationssuckin96/skippable_validation.rb",
92
92
  "lib/whyvalidationssuckin96/validation.rb",
93
93
  "lib/whyvalidationssuckin96/validation_builder.rb",
94
+ "lib/whyvalidationssuckin96/validation_collection.rb",
94
95
  "lib/whyvalidationssuckin96/validation_support.rb",
95
96
  "test/attribute_based_validation_test.rb",
96
97
  "test/macros/validates_acceptance_test.rb",
@@ -110,6 +111,7 @@ Gem::Specification.new do |s|
110
111
  "test/skippable_validation_test.rb",
111
112
  "test/teststrap.rb",
112
113
  "test/validation_builder_test.rb",
114
+ "test/validation_collection_test.rb",
113
115
  "test/validation_support_test.rb",
114
116
  "test/validation_test.rb",
115
117
  "whyvalidationssuckin96.gemspec"
@@ -138,6 +140,7 @@ Gem::Specification.new do |s|
138
140
  "test/skippable_validation_test.rb",
139
141
  "test/teststrap.rb",
140
142
  "test/validation_builder_test.rb",
143
+ "test/validation_collection_test.rb",
141
144
  "test/validation_support_test.rb",
142
145
  "test/validation_test.rb"
143
146
  ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whyvalidationssuckin96
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gabrielg
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-12-22 00:00:00 -06:00
13
+ date: 2010-01-15 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -127,6 +127,7 @@ files:
127
127
  - lib/whyvalidationssuckin96/skippable_validation.rb
128
128
  - lib/whyvalidationssuckin96/validation.rb
129
129
  - lib/whyvalidationssuckin96/validation_builder.rb
130
+ - lib/whyvalidationssuckin96/validation_collection.rb
130
131
  - lib/whyvalidationssuckin96/validation_support.rb
131
132
  - test/attribute_based_validation_test.rb
132
133
  - test/macros/validates_acceptance_test.rb
@@ -146,6 +147,7 @@ files:
146
147
  - test/skippable_validation_test.rb
147
148
  - test/teststrap.rb
148
149
  - test/validation_builder_test.rb
150
+ - test/validation_collection_test.rb
149
151
  - test/validation_support_test.rb
150
152
  - test/validation_test.rb
151
153
  - whyvalidationssuckin96.gemspec
@@ -196,5 +198,6 @@ test_files:
196
198
  - test/skippable_validation_test.rb
197
199
  - test/teststrap.rb
198
200
  - test/validation_builder_test.rb
201
+ - test/validation_collection_test.rb
199
202
  - test/validation_support_test.rb
200
203
  - test/validation_test.rb