Module: WhyValidationsSuckIn96::AttributeBasedValidation

Overview

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.

Instance Method Summary collapse

Instance Method Details

#attributeObject

The attribute to validate against



23
24
25
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 23

def attribute
  options[:attribute]
end

#attribute_valueObject

The value of the attribute to validate against



28
29
30
31
32
33
34
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 28

def attribute_value
  if options[:array]
    validatable.send(options[:attribute])[@pos]
  else
    validatable.send(options[:attribute])
  end
end

#initialize(validatable, options = {}) ⇒ Object

An initializer for a validation that checks to see if the required options have been passed for attribute based validation to work as expected.

Parameters:

  • validatable (Object)

    An object to be validated

  • options (Hash) (defaults to: {})

    The options to set up the validation with

Options Hash (options):

  • :attribute (Symbol)

    The attribute on the validatable object to validate against

  • :array (true, false)

    Specifies the attribute is an array of values to validate individually

  • :allow_nil (true, false)

    If true, skips validation of the value of the attribute is #nil?

  • :allow_blank (true, false)

    If true, skips validation of the value of the attribute is #blank?

  • :allow_empty (true, false)

    If true, along with :array, skips validation if the array is empty

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 16

def initialize(validatable, options = {})
  raise(ArgumentError, "The attribute to validate must be specified as :attribute") unless options[:attribute]
  @pos = 0 if options[:array]
  super
end

#validateObject

A default validate implementation that skips on #nil?/#blank? attribute values if :allow_nil or :allow_blank have been set.



38
39
40
41
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 38

def validate
  skip if skip_on_empty? || skip_on_blank? || skip_on_nil?
  super
end

#validates?true, ...

TODO:
  • this method is uuuuuuuugly

Performs the validation, returning true or false if the validation passes or fails, or nil if the validation will not run.

Returns:

  • (true, false, nil)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 48

def validates?
  if options[:array]
    return @passed = nil if skip_on_empty?
    return @passed = false if !skip_on_empty? && Array(validatable.send(options[:attribute])).empty?
    reset
    @pos = 0
    statuses = []
    while validating?
      status = super
      @pos += 1
      statuses << status
    end
    @passed = statuses.all?
  else
    super
  end
end