Module: ValidationMirror::ClassMethods

Defined in:
lib/validation-mirror.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_validation_method_with_capture(validation_name) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/validation-mirror.rb', line 15

def self.define_validation_method_with_capture(validation_name)
  class_eval <<-EOC
    def #{validation_name}_with_capture(*args, &block)
      #{validation_name}_without_capture(*args, &block)
      capture_validation(#{validation_name.inspect}, *args, &block)
    end
  EOC
end

.extended(base) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/validation-mirror.rb', line 26

def self.extended(base)
  base.instance_eval do 
    class_inheritable_hash :validations
    write_inheritable_hash :validations, {}
  end
  ValidationMirror::StandardValidations.each do |validation|
    (class << base; self; end).instance_eval { alias_method_chain validation, :capture }
  end
end

Instance Method Details

#capture_validation(validation_name, *args, &block) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/validation-mirror.rb', line 36

def capture_validation(validation_name, *args, &block)
  options = args.extract_options!
  attrs = args
  attrs.each do |attr|
    new_validations = (validations[attr.to_sym] || []) + [ValidationMirror::Validation.new(attr.to_sym, validation_name.to_sym, options)]
    write_inheritable_hash :validations, {attr.to_sym => new_validations}
  end
end

#reflect_all_validationsObject



49
50
51
# File 'lib/validation-mirror.rb', line 49

def reflect_all_validations
  validations.values.flatten
end

#reflect_validations_on(attr) ⇒ Object



45
46
47
# File 'lib/validation-mirror.rb', line 45

def reflect_validations_on(attr)
  validations[attr.to_sym]
end