validation-mirror 1.0.0

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 centro
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = validation-mirror
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2009 centro. See LICENSE for details.
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "validation-mirror"
8
+ gem.summary = %Q{Adds basic reflection support to ActiveRecord validations}
9
+ gem.description = %Q{Adds basic reflection support to ActiveRecord validations}
10
+ gem.email = "gabriel.gironda@gmail.com"
11
+ gem.homepage = "http://github.com/centro/validation-mirror"
12
+ gem.authors = ["centro"]
13
+ gem.add_development_dependency "riot", ">= 0"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ gem.add_dependency "activerecord", ">=2.3.5"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/*_test.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ task :test => :check_dependencies
30
+ task :default => :test
31
+
32
+ begin
33
+ require 'yard'
34
+ YARD::Rake::YardocTask.new
35
+ rescue LoadError
36
+ task :yardoc do
37
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
38
+ end
39
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,55 @@
1
+ require 'active_record'
2
+ require 'active_record/base'
3
+ require 'active_record/validations'
4
+
5
+ module ValidationMirror
6
+ StandardValidations = ActiveRecord::Validations::ClassMethods.instance_methods.grep(/_of$/)
7
+ Validation = Struct.new(:attribute, :macro, :arguments)
8
+
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ def self.define_validation_method_with_capture(validation_name)
16
+ class_eval <<-EOC
17
+ def #{validation_name}_with_capture(*args, &block)
18
+ #{validation_name}_without_capture(*args, &block)
19
+ capture_validation(#{validation_name.inspect}, *args, &block)
20
+ end
21
+ EOC
22
+ end
23
+
24
+ ValidationMirror::StandardValidations.each { |validation| define_validation_method_with_capture(validation) }
25
+
26
+ def self.extended(base)
27
+ base.instance_eval do
28
+ class_inheritable_hash :validations
29
+ write_inheritable_hash :validations, {}
30
+ end
31
+ ValidationMirror::StandardValidations.each do |validation|
32
+ (class << base; self; end).instance_eval { alias_method_chain validation, :capture }
33
+ end
34
+ end
35
+
36
+ def capture_validation(validation_name, *args, &block)
37
+ options = args.extract_options!
38
+ attrs = args
39
+ attrs.each do |attr|
40
+ new_validations = (validations[attr.to_sym] || []) + [ValidationMirror::Validation.new(attr.to_sym, validation_name.to_sym, options)]
41
+ write_inheritable_hash :validations, {attr.to_sym => new_validations}
42
+ end
43
+ end
44
+
45
+ def reflect_validations_on(attr)
46
+ validations[attr.to_sym]
47
+ end
48
+
49
+ def reflect_all_validations
50
+ validations.values.flatten
51
+ end
52
+ end # ClassMethods
53
+ end
54
+
55
+ ActiveRecord::Base.instance_eval { include ValidationMirror }
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'riot'
3
+ require 'validation-mirror'
@@ -0,0 +1,94 @@
1
+ require 'teststrap'
2
+
3
+ class TestModel < ActiveRecord::Base
4
+ validates_presence_of :some_jank, :if => lambda { |test| test == "test" }
5
+ validates_format_of :some_jank, :with => /\d/
6
+ validates_uniqueness_of :something_unique, :something_else_unique, :scope => :scope_id, :allow_nil => true
7
+
8
+ validates_numericality_of :sanity_check
9
+ validates_uniqueness_of :sanity_check
10
+ validates_inclusion_of :sanity_check, :in => 0..1
11
+ validates_size_of :sanity_check, :in => 0..1
12
+ validates_confirmation_of :sanity_check
13
+ validates_length_of :sanity_check, :in => 0..1
14
+ validates_format_of :sanity_check, :with => /\w/
15
+ validates_acceptance_of :sanity_check
16
+ validates_exclusion_of :sanity_check, :in => 0..1
17
+ validates_presence_of :sanity_check
18
+ end
19
+
20
+ context "validation-mirror" do
21
+ check_validation = lambda do |model, attr, macro_name|
22
+ model.reflect_validations_on(attr).detect { |validation| validation.macro == macro_name && validation.attribute == attr }
23
+ end
24
+
25
+ context "reflecting on all validations" do
26
+ should "return all the validations set" do
27
+ TestModel.reflect_all_validations.size
28
+ end.equals(14)
29
+ end
30
+
31
+ context "reflecting on validations for a single attribute" do
32
+ setup { TestModel }
33
+
34
+ should "capture all the standard AR validations we know of" do
35
+ topic.reflect_validations_on(:sanity_check).size
36
+ end.equals(10)
37
+
38
+ context "asserting validation presence" do
39
+
40
+ should "have a validates_presence_of on some_jank" do
41
+ check_validation[TestModel, :some_jank, :validates_presence_of]
42
+ end
43
+
44
+ should "have a validates_format_of on some_jank" do
45
+ check_validation[TestModel, :some_jank, :validates_format_of]
46
+ end
47
+
48
+ should "have a uniqueness validation on something_unique" do
49
+ check_validation[TestModel, :something_unique, :validates_uniqueness_of]
50
+ end
51
+
52
+ should "have a uniqueness validation on something_else_unique" do
53
+ check_validation[TestModel, :something_else_unique, :validates_uniqueness_of]
54
+ end
55
+
56
+ end # asserting validation presence
57
+
58
+ context "asserting validation setup arguments" do
59
+
60
+ should "have the correct arguments length for validates_presence_of on :some_jank" do
61
+ validation = check_validation[TestModel, :some_jank, :validates_presence_of]
62
+ validation.arguments.size
63
+ end.equals(1)
64
+
65
+ should "have stashed away the :if block on validates_presence_of for :some_jank" do
66
+ validation = check_validation[TestModel, :some_jank, :validates_presence_of]
67
+ validation.arguments[:if].call("test")
68
+ end
69
+
70
+ should "have the correct arguments length for validates_format_of on :some_jank" do
71
+ validation = check_validation[TestModel, :some_jank, :validates_format_of]
72
+ validation.arguments.size
73
+ end.equals(1)
74
+
75
+ should "have stashed away the arguments on validates_format_of for :some_jank" do
76
+ validation = check_validation[TestModel, :some_jank, :validates_format_of]
77
+ validation.arguments
78
+ end.equals(:with => /\d/)
79
+
80
+ should "have stashed away the arguments on validates_uniqueness_of for :something_unique" do
81
+ validation = check_validation[TestModel, :something_unique, :validates_uniqueness_of]
82
+ validation.arguments
83
+ end.equals(:scope => :scope_id, :allow_nil => true)
84
+
85
+ should "have stashed away the arguments on validates_uniqueness_of for :something_else_unique" do
86
+ validation = check_validation[TestModel, :something_else_unique, :validates_uniqueness_of]
87
+ validation.arguments
88
+ end.equals(:scope => :scope_id, :allow_nil => true)
89
+
90
+ end # asserting validation setup arguments
91
+
92
+ end # reflecting on validations for a single attribute
93
+
94
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validation-mirror
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - centro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-16 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: riot
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: yard
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.3.5
44
+ version:
45
+ description: Adds basic reflection support to ActiveRecord validations
46
+ email: gabriel.gironda@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/validation-mirror.rb
62
+ - test/teststrap.rb
63
+ - test/validation-mirror_test.rb
64
+ has_rdoc: true
65
+ homepage: http://github.com/centro/validation-mirror
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --charset=UTF-8
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.5
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Adds basic reflection support to ActiveRecord validations
92
+ test_files:
93
+ - test/teststrap.rb
94
+ - test/validation-mirror_test.rb