Class: Validatable::ValidationBase

Inherits:
Object
  • Object
show all
Includes:
Requireable, Understandable
Defined in:
lib/mack-notifier/validations.rb,
lib/gems/validatable-1.6.7/lib/validations/validation_base.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Requireable

included, #requires

Methods included from Understandable

included, #must_understand

Constructor Details

#initialize(klass, attribute, options = {}) ⇒ ValidationBase

Returns a new instance of ValidationBase.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 48

def initialize(klass, attribute, options={})
  must_understand options
  requires options
  self.class.all_understandings.each do |understanding|
    options[understanding] = self.class.all_defaults[understanding] unless options.has_key? understanding
    self.instance_variable_set("@#{understanding}", options[understanding])
  end
  self.attribute = attribute
  self.groups = [self.groups] unless self.groups.is_a?(Array)
  self.key = "#{klass.name}/#{self.class.name}/#{self.key || self.attribute}"
  raise_error_if_key_is_dup(klass)
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



46
47
48
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 46

def attribute
  @attribute
end

Class Method Details

.after_validate(&block) ⇒ Object



27
28
29
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 27

def after_validate(&block)
  after_validations << block
end

.after_validationsObject



31
32
33
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 31

def after_validations
  @after_validations ||= []
end

.all_after_validationsObject



35
36
37
38
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 35

def all_after_validations
  return after_validations + self.superclass.all_after_validations if self.superclass.respond_to? :all_after_validations
  after_validations
end

.all_defaultsObject



22
23
24
25
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 22

def all_defaults
  return defaults.merge(self.superclass.all_defaults) if self.superclass.respond_to? :all_defaults
  defaults
end

.default(hash) ⇒ Object



14
15
16
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 14

def default(hash)
  defaults.merge! hash
end

.defaultsObject



18
19
20
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 18

def defaults
  @defaults ||= {}
end

.option(*args) ⇒ Object



9
10
11
12
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 9

def option(*args)
  attr_accessor(*args)
  understands(*args)
end

.required_option(*args) ⇒ Object



4
5
6
7
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 4

def required_option(*args)
  option(*args)
  requires(*args)
end

Instance Method Details

#message(instance) ⇒ Object



72
73
74
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 72

def message(instance)
  @message.respond_to?(:call) ? instance.instance_eval(&@message) : @message
end

#raise_error_if_key_is_dup(klass) ⇒ Object

This fixes a bug with reloading of Validatable classes.

Raises:



96
97
98
99
100
101
102
103
104
105
# File 'lib/mack-notifier/validations.rb', line 96

def raise_error_if_key_is_dup(klass) # :nodoc:
  vals = {}
  klass.validations.each do |v|
    vals[v.key] = v
  end
  klass.validations.clear
  vals.each do |k,v|
    klass.validations << v
  end
end

#run_after_validate(result, instance, attribute) ⇒ Object



81
82
83
84
85
86
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 81

def run_after_validate(result, instance, attribute)
  self.class.all_after_validations.each do |block|
    block.call result, instance, attribute
  end
  instance.instance_eval_with_params result, attribute, &self.after_validate unless self.after_validate.nil?
end

#should_validate?(instance) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 66

def should_validate?(instance)
  result = validate_this_time?(instance)
  result &&= instance.instance_eval(&self.if) unless self.if.nil?
  result
end

#validate_this_time?(instance) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/gems/validatable-1.6.7/lib/validations/validation_base.rb', line 76

def validate_this_time?(instance)
  return true if @times.nil?
  self.times > instance.times_validated(self.key)
end