Sha256: 076d7b68c4ce6e9a82218b240ed77c01e4297bbc0a7845ec1e4b31df2f7d9ed6

Contents?: true

Size: 1.83 KB

Versions: 15

Compression:

Stored size: 1.83 KB

Contents

module WhyValidationsSuckIn96
  # A mixin to help handle the most common case of validating a single attribute on an object. This module has a 
  # dependency on SkippableValidation that will most likely be removed in future releases, but is something to be
  # aware of currently.
  module AttributeBasedValidation
    
    # An initializer for a validation that checks to see if the required options have been passed for
    # attribute based validation to work as expected.
    # @param  [Object] validatable                An object to be validated
    # @param  [Hash]   options                    The options to set up the validation with
    # @option options  [Symbol]      :attribute   The attribute on the validatable object to validate against
    # @option options  [true, false] :allow_nil   If true, skips validation of the value of the attribute is #nil?
    # @option options  [true, false] :allow_blank If true, skips validation of the value of the attribute is #blank?
    def initialize(validatable, options = {})
      raise(ArgumentError, "The attribute to validate must be specified as :attribute") unless options[:attribute]
      super
    end
    
    # The attribute to validate against
    def attribute
      options[:attribute]
    end
    
    # The value of the attribute to validate against
    def attribute_value
      validatable.send(options[:attribute])
    end
    
    # A default validate implementation that skips on #nil?/#blank? attribute values if :allow_nil or :allow_blank
    # have been set.
    def validate
      skip if skip_on_blank? || skip_on_nil?
      super
    end
    
  private

    def skip_on_nil?
      options[:allow_nil] && attribute_value.nil?
    end
    
    def skip_on_blank?
      options[:allow_blank] && attribute_value.blank?
    end
  end # AttributeBasedValidation
end   # WhyValidationsSuckIn96

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
whyvalidationssuckin96-1.5.5 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.5.4 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.5.3 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.5.2 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.5.1 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.5.0 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.4.0 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.3.1 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.3.0 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.2.3 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.2.2 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.2.1 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.2.0 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.1.0 lib/whyvalidationssuckin96/attribute_based_validation.rb
whyvalidationssuckin96-1.0.0 lib/whyvalidationssuckin96/attribute_based_validation.rb