validatable 1.6.3 → 1.6.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -227,5 +227,9 @@ module Validatable
227
227
  def include_validations_for(attribute_to_validate, options = {})
228
228
  children_to_validate << ChildValidation.new(attribute_to_validate, options[:map] || {}, options[:if] || lambda { true })
229
229
  end
230
+
231
+ def before_validation(&block)
232
+ before_validations << block
233
+ end
230
234
  end
231
235
  end
@@ -0,0 +1,20 @@
1
+ class Object
2
+ module InstanceExecHelper; end
3
+ include InstanceExecHelper
4
+ def instance_eval_with_params(*args, &block)
5
+ begin
6
+ old_critical, Thread.critical = Thread.critical, true
7
+ n = 0
8
+ n += 1 while respond_to?(mname="__instance_exec#{n}")
9
+ InstanceExecHelper.module_eval{ define_method(mname, &block) }
10
+ ensure
11
+ Thread.critical = old_critical
12
+ end
13
+ begin
14
+ ret = send(mname, *args)
15
+ ensure
16
+ InstanceExecHelper.module_eval{ remove_method(mname) } rescue nil
17
+ end
18
+ ret
19
+ end
20
+ end
@@ -1,4 +1,5 @@
1
1
  require 'forwardable'
2
+ require File.expand_path(File.dirname(__FILE__) + '/object_extension')
2
3
  require File.expand_path(File.dirname(__FILE__) + '/errors')
3
4
  require File.expand_path(File.dirname(__FILE__) + '/validatable_class_methods')
4
5
  require File.expand_path(File.dirname(__FILE__) + '/macros')
@@ -21,6 +21,17 @@ module Validatable
21
21
  end
22
22
  end
23
23
  end
24
+
25
+ def all_before_validations
26
+ if self.superclass.respond_to? :all_before_validations
27
+ return before_validations + self.superclass.all_before_validations
28
+ end
29
+ before_validations
30
+ end
31
+
32
+ def before_validations
33
+ @before_validations ||= []
34
+ end
24
35
 
25
36
  def all_validations
26
37
  return validations + self.superclass.all_validations if self.superclass.respond_to? :all_validations
@@ -19,6 +19,7 @@ module Validatable
19
19
  end
20
20
 
21
21
  def valid_for_group?(group) #:nodoc:
22
+ run_before_validations
22
23
  errors.clear
23
24
  self.class.validate_children(self, group)
24
25
  self.validate(group)
@@ -60,6 +61,12 @@ module Validatable
60
61
  validation.run_after_validate(validation_result, self, validation.attribute)
61
62
  end
62
63
 
64
+ def run_before_validations
65
+ self.class.all_before_validations.each do |block|
66
+ instance_eval &block
67
+ end
68
+ end
69
+
63
70
  def add_error(attribute, message) #:nodoc:
64
71
  self.class.add_error(self, attribute, message)
65
72
  end
@@ -82,7 +82,7 @@ module Validatable
82
82
  self.class.all_after_validations.each do |block|
83
83
  block.call result, instance, attribute
84
84
  end
85
- self.after_validate.call result, instance, attribute unless self.after_validate.nil?
85
+ instance.instance_eval_with_params result, attribute, &self.after_validate unless self.after_validate.nil?
86
86
  end
87
87
  end
88
88
  end
@@ -29,7 +29,7 @@ Gem::manage_gems
29
29
  specification = Gem::Specification.new do |s|
30
30
  s.name = "validatable"
31
31
  s.summary = "Validatable is a library for adding validations."
32
- s.version = "1.6.3"
32
+ s.version = "1.6.4"
33
33
  s.author = 'Jay Fields'
34
34
  s.description = "Validatable is a library for adding validations."
35
35
  s.email = 'validatable-developer@rubyforge.org'
@@ -1,12 +1,25 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  functional_tests do
4
-
4
+ expect :is_set do
5
+ klass = Class.new do
6
+ include Validatable
7
+ attr_accessor :result
8
+ before_validation do
9
+ self.result = :is_set
10
+ end
11
+ end
12
+
13
+ instance = klass.new
14
+ instance.valid?
15
+ instance.result
16
+ end
17
+
5
18
  expect :is_set do
6
19
  klass = Class.new do
7
20
  include Validatable
8
21
  attr_accessor :name, :result
9
- validates_presence_of :name, :after_validate => lambda { |result, instance, attribute| instance.result = :is_set }
22
+ validates_presence_of :name, :after_validate => lambda { |result, attribute| self.result = :is_set }
10
23
  end
11
24
 
12
25
  instance = klass.new
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: validatable
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.6.3
6
+ version: 1.6.4
7
7
  date: 2007-08-15 00:00:00 -04:00
8
8
  summary: Validatable is a library for adding validations.
9
9
  require_paths:
@@ -32,6 +32,7 @@ files:
32
32
  - lib/child_validation.rb
33
33
  - lib/errors.rb
34
34
  - lib/macros.rb
35
+ - lib/object_extension.rb
35
36
  - lib/requireable.rb
36
37
  - lib/understandable.rb
37
38
  - lib/validatable.rb