Sha256: eff8b8894e8ae082e1da2df268999c03f9733ac5f02d4f97b8ead0b058c01941

Contents?: true

Size: 1.65 KB

Versions: 20

Compression:

Stored size: 1.65 KB

Contents

module WhyValidationsSuckIn96
  # A mixin to handle specifying :if and :unless options to check before performing validation.
  #
  # @example Validate if a given block returns true
  #   setup_validations do
  #     validates_associated :tracks, :if => lambda { !validatable.tracks.empty? }
  #   end
  #
  # @example Validate if a given method on the validatable object returns true
  #   setup_validations do
  #     validates_associated :tracks, :if => :allow_validation
  #   end
  #   
  #   def allow_validation
  #     false
  #   end
  #
  # @example Validate unless a given block returns true
  #   setup_validations do
  #     validates_associated :tracks, :unless => lambda { validatable.tracks.empty? }
  #   end
  #
  # @example Validate unless a given method on the validatable object returns true
  #   setup_validations do
  #     validates_associated :tracks, :unless => :disallow_validation
  #   end
  #   
  #   def disallow_validation
  #     true
  #   end
  module SkippableValidation
    
    def validate
      skip if skip_validation?
    end
    
  private
  
    def skip
      throw :validation_done, nil
    end
    
    def skip_validation?
      skip_if? || skip_unless?
    end
    
    def skip_unless?
      return false unless options.has_key?(:unless)
      check = options[:unless].is_a?(Proc) ? instance_eval(&options[:unless]) : validatable.send(options[:unless])
      check == true
    end
    
    def skip_if?
      return false unless options.has_key?(:if)
      check = options[:if].is_a?(Proc) ? instance_eval(&options[:if]) : validatable.send(options[:if])
      check == false
    end
  end # SkippableValidation
end   # WhyValidationsSuckIn96

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
whyvalidationssuckin96-1.6.4 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.6.3 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.6.2 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.6.1 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.6.0 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.5.5 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.5.4 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.5.3 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.5.2 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.5.1 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.5.0 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.4.0 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.3.1 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.3.0 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.2.3 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.2.2 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.2.1 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.2.0 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.1.0 lib/whyvalidationssuckin96/skippable_validation.rb
whyvalidationssuckin96-1.0.0 lib/whyvalidationssuckin96/skippable_validation.rb