validation_reflection 0.3.3 → 0.3.4

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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_STORE
2
+ *~
3
+ pkg*
4
+ validation_reflection*.gem
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ == 0.3.3, 2009-09-12
2
+ * version bump
3
+
1
4
  == 0.3.2, 2009-09-12
2
5
  * gemified by Christopher Redinger
3
6
 
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'rake'
2
3
  require 'rake/testtask'
3
4
  require 'rake/rdoctask'
@@ -22,7 +23,12 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
22
23
  end
23
24
 
24
25
  begin
25
- require 'jeweler'
26
+ begin
27
+ require 'jeweler'
28
+ rescue LoadError
29
+ gem 'technicalpickles-jeweler', '>= 1.0.0'
30
+ require 'jeweler'
31
+ end
26
32
  Jeweler::Tasks.new do |gemspec|
27
33
  gemspec.name = "validation_reflection"
28
34
  gemspec.summary = "Adds reflective access to validations"
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
+ :major: 0
2
3
  :minor: 3
3
- :patch: 3
4
- :major: 0
4
+ :patch: 4
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  #--
2
3
  # Copyright (c) 2006-2008, Michael Schuerig, michael@schuerig.de
3
4
  #
@@ -20,59 +21,70 @@
20
21
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
22
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23
  #++
23
-
24
-
25
24
  require 'active_record/reflection'
26
25
  require 'ostruct'
27
26
 
28
27
  # Based on code by Sebastian Kanthak
29
28
  # See http://dev.rubyonrails.org/ticket/861
29
+ #
30
30
  module ActiveRecordExtensions # :nodoc:
31
31
  module ValidationReflection # :nodoc:
32
- CONFIG_PATH = File.join(RAILS_ROOT, 'config', 'plugins', 'validation_reflection.rb')
33
-
34
- mattr_accessor :reflected_validations
35
- ActiveRecordExtensions::ValidationReflection.reflected_validations = %w(
36
- validates_acceptance_of
37
- validates_associated
38
- validates_confirmation_of
39
- validates_exclusion_of
40
- validates_format_of
41
- validates_inclusion_of
42
- validates_length_of
43
- validates_numericality_of
44
- validates_presence_of
45
- validates_uniqueness_of
46
- )
47
-
48
- mattr_accessor :in_ignored_subvalidation
49
- ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation = false
50
-
51
- def self.included(base)
52
- return if base.kind_of?(ActiveRecordExtensions::ValidationReflection::ClassMethods)
32
+
33
+ extend self
34
+
35
+ # Look for config/initalizer here in:
36
+ CONFIG_PATH = ::Dir.glob((::File.join(RAILS_ROOT, 'config', '**', 'validation_reflection.rb').to_s rescue '')).first || ''
37
+ CORE_VALIDATONS = [
38
+ :validates_acceptance_of,
39
+ :validates_associated,
40
+ :validates_confirmation_of,
41
+ :validates_exclusion_of,
42
+ :validates_format_of,
43
+ :validates_inclusion_of,
44
+ :validates_length_of,
45
+ :validates_numericality_of,
46
+ :validates_presence_of,
47
+ :validates_uniqueness_of,
48
+ ].freeze
49
+
50
+ @@reflected_validations = CORE_VALIDATONS.dup
51
+ @@in_ignored_subvalidation = false
52
+
53
+ mattr_accessor :reflected_validations,
54
+ :in_ignored_subvalidation
55
+
56
+ def included(base) # :nodoc:
57
+ return if base.kind_of?(::ActiveRecordExtensions::ValidationReflection::ClassMethods)
53
58
  base.extend(ClassMethods)
54
59
  end
55
60
 
56
- def self.load_config
57
- if File.file?(CONFIG_PATH)
58
- config = OpenStruct.new
59
- config.reflected_validations = reflected_validations
61
+ # Load config/initializer on load, where ValidationReflection defaults
62
+ # (such as which validations to reflect upon) cane be overridden/extended.
63
+ #
64
+ def load_config
65
+ if ::File.file?(CONFIG_PATH)
66
+ config = ::OpenStruct.new
67
+ config.reflected_validations = @@reflected_validations
60
68
  silence_warnings do
61
- eval(IO.read(CONFIG_PATH), binding, CONFIG_PATH)
69
+ eval(::IO.read(CONFIG_PATH), binding, CONFIG_PATH)
62
70
  end
63
71
  end
64
72
  end
65
73
 
66
- def self.install(base)
67
- reflected_validations.freeze
68
- reflected_validations.each do |validation_type|
69
- next if base.respond_to?("#{validation_type}_with_reflection")
74
+ # Iterate through all validations and store/cache the info
75
+ # for later easy access.
76
+ #
77
+ def install(base)
78
+ @@reflected_validations.each do |validation_type|
79
+ next if base.respond_to?(:"#{validation_type}_with_reflection")
70
80
  ignore_subvalidations = false
71
- if validation_type.kind_of?(Hash)
81
+
82
+ if validation_type.kind_of?(::Hash)
72
83
  ignore_subvalidations = validation_type[:ignore_subvalidations]
73
84
  validation_type = validation_type[:method]
74
85
  end
75
- base.class_eval <<-"end_eval"
86
+
87
+ base.class_eval %{
76
88
  class << self
77
89
  def #{validation_type}_with_reflection(*attr_names)
78
90
  ignoring_subvalidations(#{ignore_subvalidations}) do
@@ -80,53 +92,57 @@ module ActiveRecordExtensions # :nodoc:
80
92
  remember_validation_metadata(:#{validation_type}, *attr_names)
81
93
  end
82
94
  end
83
-
84
95
  alias_method_chain :#{validation_type}, :reflection
85
96
  end
86
- end_eval
97
+ }, __FILE__, __LINE__
87
98
  end
88
99
  end
100
+ alias :reload :install
89
101
 
90
102
  module ClassMethods
91
103
 
104
+ include ::ActiveRecordExtensions::ValidationReflection
105
+
92
106
  # Returns an array of MacroReflection objects for all validations in the class
93
107
  def reflect_on_all_validations
94
- read_inheritable_attribute(:validations) || []
108
+ self.read_inheritable_attribute(:validations) || []
95
109
  end
96
110
 
97
111
  # Returns an array of MacroReflection objects for all validations defined for the field +attr_name+.
98
112
  def reflect_on_validations_for(attr_name)
99
- attr_name = attr_name.to_sym
100
- reflect_on_all_validations.select do |reflection|
101
- reflection.name == attr_name
113
+ self.reflect_on_all_validations.select do |reflection|
114
+ reflection.name == attr_name.to_sym
102
115
  end
103
116
  end
104
117
 
105
118
  private
106
-
107
- def remember_validation_metadata(validation_type, *attr_names)
108
- configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
109
- attr_names.each do |attr_name|
110
- write_inheritable_array :validations,
111
- [ ActiveRecord::Reflection::MacroReflection.new(validation_type, attr_name.to_sym, configuration, self) ]
119
+
120
+ # Store validation info for easy and fast access.
121
+ #
122
+ def remember_validation_metadata(validation_type, *attr_names)
123
+ configuration = attr_names.last.is_a?(::Hash) ? attr_names.pop : {}
124
+ attr_names.each do |attr_name|
125
+ self.write_inheritable_array :validations,
126
+ [::ActiveRecord::Reflection::MacroReflection.new(validation_type, attr_name.to_sym, configuration, self)]
127
+ end
112
128
  end
113
- end
114
-
115
- def ignoring_subvalidations(ignore)
116
- save_ignore = ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation
117
- unless ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation
118
- ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation = ignore
119
- yield
129
+
130
+ def ignoring_subvalidations(ignore)
131
+ save_ignore = self.in_ignored_subvalidation
132
+ unless self.in_ignored_subvalidation
133
+ self.in_ignored_subvalidation = ignore
134
+ yield
135
+ end
136
+ ensure
137
+ self.in_ignored_subvalidation = save_ignore
120
138
  end
121
- ensure
122
- ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation = save_ignore
123
- end
139
+
124
140
  end
125
141
  end
126
142
  end
127
143
 
128
144
  ActiveRecord::Base.class_eval do
129
- include ActiveRecordExtensions::ValidationReflection
130
- ActiveRecordExtensions::ValidationReflection.load_config
131
- ActiveRecordExtensions::ValidationReflection.install(self)
145
+ include ::ActiveRecordExtensions::ValidationReflection
146
+ ::ActiveRecordExtensions::ValidationReflection.load_config
147
+ ::ActiveRecordExtensions::ValidationReflection.install(self)
132
148
  end
data/rails/init.rb CHANGED
@@ -1 +1,2 @@
1
+ # encoding: utf-8
1
2
  require 'validation_reflection'
data/test/test_helper.rb CHANGED
@@ -1,6 +1,19 @@
1
- ENV["RAILS_ENV"] = "test"
1
+ # encoding: utf-8
2
+ ENV['RAILS_ENV'] = 'test'
2
3
  RAILS_ROOT = File.join(File.dirname(__FILE__))
3
4
 
4
5
  require 'rubygems'
5
- require 'test/unit'
6
- require 'active_record'
6
+
7
+ begin
8
+ require 'active_record'
9
+ rescue LoadError
10
+ gem 'activerecord', '>= 1.2.3'
11
+ require 'active_record'
12
+ end
13
+
14
+ begin
15
+ require 'test/unit'
16
+ rescue LoadError
17
+ gem 'test-unit', '>= 1.2.3'
18
+ require 'test/unit'
19
+ end
@@ -1,4 +1,5 @@
1
- require 'test_helper'
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
3
 
3
4
  ActiveRecord::Base.class_eval do
4
5
  def self.validates_something_weird(*cols)
@@ -16,13 +17,13 @@ end
16
17
  require 'validation_reflection'
17
18
 
18
19
  ActiveRecord::Base.class_eval do
19
- include ActiveRecordExtensions::ValidationReflection
20
- ActiveRecordExtensions::ValidationReflection.reflected_validations << :validates_something_weird
21
- ActiveRecordExtensions::ValidationReflection.reflected_validations << {
22
- :method => :validates_something_selfcontained,
23
- :ignore_subvalidations => true
24
- }
25
- ActiveRecordExtensions::ValidationReflection.install(self)
20
+ include ::ActiveRecordExtensions::ValidationReflection
21
+ ::ActiveRecordExtensions::ValidationReflection.reflected_validations << :validates_something_weird
22
+ ::ActiveRecordExtensions::ValidationReflection.reflected_validations << {
23
+ :method => :validates_something_selfcontained,
24
+ :ignore_subvalidations => true
25
+ }
26
+ ::ActiveRecordExtensions::ValidationReflection.install(self)
26
27
  end
27
28
 
28
29
 
@@ -30,13 +31,13 @@ class ValidationReflectionTest < Test::Unit::TestCase
30
31
 
31
32
  class Dummy < ActiveRecord::Base
32
33
  class << self
33
-
34
+
34
35
  def create_fake_column(name, null = true, limit = nil)
35
36
  sql_type = limit ? "varchar (#{limit})" : nil
36
37
  col = ActiveRecord::ConnectionAdapters::Column.new(name, nil, sql_type, null)
37
38
  col
38
39
  end
39
-
40
+
40
41
  def columns
41
42
  [
42
43
  create_fake_column('col0'),
@@ -50,9 +51,9 @@ class ValidationReflectionTest < Test::Unit::TestCase
50
51
  ]
51
52
  end
52
53
  end
53
-
54
+
54
55
  has_one :nothing
55
-
56
+
56
57
  validates_presence_of :col1
57
58
  validates_length_of :col2, :maximum => 100
58
59
  validates_format_of :col3, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
@@ -62,61 +63,61 @@ class ValidationReflectionTest < Test::Unit::TestCase
62
63
  validates_something_selfcontained :col7
63
64
  end
64
65
 
65
-
66
66
  def test_sanity
67
67
  assert_equal [], Dummy.reflect_on_validations_for(:col0)
68
68
  end
69
69
 
70
70
  def test_validates_presence_of_is_reflected
71
- refls = Dummy.reflect_on_validations_for(:col1)
72
- assert refls.all? { |r| r.name.to_s == 'col1' }
73
- assert refls.find { |r| r.macro == :validates_presence_of }
71
+ reflections = Dummy.reflect_on_validations_for(:col1)
72
+ assert reflections.all? { |r| r.name.to_s == 'col1' }
73
+ assert reflections.find { |r| r.macro == :validates_presence_of }
74
74
  end
75
75
 
76
76
  def test_string_limit_is_reflected
77
- refls = Dummy.reflect_on_validations_for(:col2)
78
- assert refls.any? { |r| r.macro == :validates_length_of && r.options[:maximum] == 100 }
77
+ reflections = Dummy.reflect_on_validations_for(:col2)
78
+ assert reflections.any? { |r| r.macro == :validates_length_of && r.options[:maximum] == 100 }
79
79
  end
80
80
 
81
81
  def test_format_is_reflected
82
- refls = Dummy.reflect_on_validations_for(:col3)
83
- assert refls.any? { |r| r.macro == :validates_format_of && r.options[:with] == /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
82
+ reflections = Dummy.reflect_on_validations_for(:col3)
83
+ assert reflections.any? { |r| r.macro == :validates_format_of && r.options[:with] == /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
84
84
  end
85
85
 
86
86
  def test_numeric_integer_is_reflected
87
- refls = Dummy.reflect_on_validations_for(:col4)
88
- assert refls.any? { |r| r.macro == :validates_numericality_of && r.options[:only_integer] }
87
+ reflections = Dummy.reflect_on_validations_for(:col4)
88
+ assert reflections.any? { |r| r.macro == :validates_numericality_of && r.options[:only_integer] }
89
89
  end
90
-
90
+
91
91
  def test_numeric_is_reflected
92
- refls = Dummy.reflect_on_validations_for(:col5)
93
- assert refls.any? { |r| r.macro == :validates_numericality_of }
92
+ reflections = Dummy.reflect_on_validations_for(:col5)
93
+ assert reflections.any? { |r| r.macro == :validates_numericality_of }
94
94
  end
95
-
95
+
96
96
  def test_validation_options_are_reflected
97
- refls = Dummy.reflect_on_validations_for(:col5)
98
- refl = refls[0]
97
+ reflections = Dummy.reflect_on_validations_for(:col5)
98
+ refl = reflections[0]
99
99
  assert_equal 5, refl.options[:less_than]
100
100
  end
101
101
 
102
102
  def test_custom_validations_are_reflected
103
- refls = Dummy.reflect_on_validations_for(:col6)
104
- assert refls.any? { |r| r.macro == :validates_something_weird }
105
- assert refls.any? { |r| r.macro == :validates_format_of }
103
+ reflections = Dummy.reflect_on_validations_for(:col6)
104
+ assert reflections.any? { |r| r.macro == :validates_something_weird }
105
+ assert reflections.any? { |r| r.macro == :validates_format_of }
106
106
  end
107
107
 
108
108
  def test_custom_validations_with_options_are_reflected
109
- refls = Dummy.reflect_on_validations_for(:col7)
110
- assert refls.any? { |r| r.macro == :validates_something_selfcontained }
109
+ reflections = Dummy.reflect_on_validations_for(:col7)
110
+ assert reflections.any? { |r| r.macro == :validates_something_selfcontained }
111
111
  end
112
112
 
113
113
  def test_subvalidations_are_reflected
114
- refls = Dummy.reflect_on_validations_for(:col6)
115
- assert_equal 2, refls.size
114
+ reflections = Dummy.reflect_on_validations_for(:col6)
115
+ assert_equal 2, reflections.size
116
116
  end
117
117
 
118
118
  def test_ignored_subvalidations_are_not_reflected
119
- refls = Dummy.reflect_on_validations_for(:col7)
120
- assert_equal 1, refls.size
119
+ reflections = Dummy.reflect_on_validations_for(:col7)
120
+ assert_equal 1, reflections.size
121
121
  end
122
+
122
123
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{validation_reflection}
8
- s.version = "0.3.3"
8
+ s.version = "0.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Christopher Redinger"]
12
- s.date = %q{2009-09-12}
12
+ s.date = %q{2009-10-09}
13
13
  s.description = %q{Adds reflective access to validations}
14
14
  s.email = %q{redinger@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -17,7 +17,8 @@ Gem::Specification.new do |s|
17
17
  "README"
18
18
  ]
19
19
  s.files = [
20
- "CHANGELOG",
20
+ ".gitignore",
21
+ "CHANGELOG",
21
22
  "LICENSE",
22
23
  "README",
23
24
  "Rakefile",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validation_reflection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Redinger
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-12 00:00:00 -04:00
12
+ date: 2009-10-09 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,6 +23,7 @@ extra_rdoc_files:
23
23
  - LICENSE
24
24
  - README
25
25
  files:
26
+ - .gitignore
26
27
  - CHANGELOG
27
28
  - LICENSE
28
29
  - README