Class: WhyValidationsSuckIn96::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/whyvalidationssuckin96/validation.rb

Overview

Base class to use when implementing validations.

Constant Summary

DefaultOptions = A hash of default options for the validation to use.
{}

Class Attribute Summary

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Constructor Details

- (Validation) initialize(validatable, options = {})

A new instance of Validation

Parameters:

  • (Object) validatable — An object to be validated
  • (Hash) options (defaults to: {}) — The options to set up the validation with


21
22
23
24
# File 'lib/whyvalidationssuckin96/validation.rb', line 21

def initialize(validatable, options = {})
  @validatable = validatable
  @options = self.class::DefaultOptions.merge(options)
end

Class Attribute Details

+ (Object) name

Returns the value of attribute name



16
17
18
# File 'lib/whyvalidationssuckin96/validation.rb', line 16

def name
  @name
end

Instance Attribute Details

- (Object) options

The options the validation was initialized with



10
11
12
# File 'lib/whyvalidationssuckin96/validation.rb', line 10

def options
  @options
end

- (Object) validatable (readonly)

The object the validation is validating



13
14
15
# File 'lib/whyvalidationssuckin96/validation.rb', line 13

def validatable
  @validatable
end

Class Method Details

+ (Object) new_subclass(name, def_block)

Creates a new subclass of this class, used when defining custom validations with a block



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/whyvalidationssuckin96/validation.rb', line 27

def self.new_subclass(name, def_block)
  Class.new(self) do
    self.name = name.to_sym
    define_method(:validate, &def_block)
    private :validate

    def inspect
      "#<WhyValidationsSuckIn96::Validation subclass for validating '#{self.class.name}'> #{super}"
    end
  end
end

Instance Method Details

- (true, false) failed?

Has this validation failed?

Returns:

  • (true, false)


47
48
49
# File 'lib/whyvalidationssuckin96/validation.rb', line 47

def failed?
  @passed == false
end

- (true, false) has_run?

Has this validation run?

Returns:

  • (true, false)


53
54
55
# File 'lib/whyvalidationssuckin96/validation.rb', line 53

def has_run?
  @passed != nil
end

- (Object) message

The failure message for this validation.



69
70
71
# File 'lib/whyvalidationssuckin96/validation.rb', line 69

def message
  @options[:message] || "failed validation"
end

- (true, false) passed?

Has this validation passed?

Returns:

  • (true, false)


41
42
43
# File 'lib/whyvalidationssuckin96/validation.rb', line 41

def passed?
  @passed == true
end

- (true, ...) validates?

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)


60
61
62
63
64
65
66
# File 'lib/whyvalidationssuckin96/validation.rb', line 60

def validates?
  reset
  @passed = catch :validation_done do
    validate
    pass
  end
end