Class: WhyValidationsSuckIn96::Validation
- Inherits:
-
Object
- Object
- WhyValidationsSuckIn96::Validation
- 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
- + (Object) name Returns the value of attribute name.
Instance Attribute Summary
- - (Object) options The options the validation was initialized with.
- - (Object) validatable readonly The object the validation is validating.
Class Method Summary
- + (Object) new_subclass(name, def_block) Creates a new subclass of this class, used when defining custom validations with a block.
Instance Method Summary
- - (true, false) failed? Has this validation failed?.
- - (true, false) has_run? Has this validation run?.
- - (Validation) initialize(validatable, options = {}) constructor A new instance of Validation.
- - (Object) message The failure message for this validation.
- - (true, false) passed? Has this validation passed?.
- - (true, ...) validates? Performs the validation, returning true or false if the validation passes or fails, or nil if the validation will not run.
Constructor Details
- (Validation) initialize(validatable, options = {})
A new instance of Validation
21 22 23 24 |
# File 'lib/whyvalidationssuckin96/validation.rb', line 21 def initialize(validatable, = {}) @validatable = validatable = self.class::DefaultOptions.merge() 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 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?
47 48 49 |
# File 'lib/whyvalidationssuckin96/validation.rb', line 47 def failed? @passed == false end |
- (true, false) has_run?
Has this validation run?
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] || "failed validation" end |
- (true, false) passed?
Has this validation passed?
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.
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 |