Class: Exocora::Validation
- Inherits:
-
Object
- Object
- Exocora::Validation
- Defined in:
- lib/exocora/validation.rb
Overview
Validates a parameter. If a block is provided, the block is applied to values passed to apply; the return value determines if the value is valid. Otherwise, compares filter === value.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_MESSAGE =
'must be present'
Instance Attribute Summary collapse
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#message ⇒ Object
Returns the value of attribute message.
-
#negate ⇒ Object
Returns the value of attribute negate.
Instance Method Summary collapse
-
#because(message) ⇒ Object
(also: #explain)
Change the message given when this validation fails.
-
#if(filter, &block) ⇒ Object
(also: #with, #when)
Sets the filter used.
-
#initialize(filter = Object, message = DEFAULT_MESSAGE) ⇒ Validation
constructor
A new instance of Validation.
- #to_s ⇒ Object
-
#unless(filter, &block) ⇒ Object
Adds the opposite of this filter.
-
#validate(value) ⇒ Object
Applies this validation to a valie.
Constructor Details
#initialize(filter = Object, message = DEFAULT_MESSAGE) ⇒ Validation
Returns a new instance of Validation.
11 12 13 14 15 |
# File 'lib/exocora/validation.rb', line 11 def initialize(filter=Object, =DEFAULT_MESSAGE) @filter = filter @message = @negate = false end |
Instance Attribute Details
#filter ⇒ Object
Returns the value of attribute filter.
9 10 11 |
# File 'lib/exocora/validation.rb', line 9 def filter @filter end |
#message ⇒ Object
Returns the value of attribute message.
9 10 11 |
# File 'lib/exocora/validation.rb', line 9 def @message end |
#negate ⇒ Object
Returns the value of attribute negate.
9 10 11 |
# File 'lib/exocora/validation.rb', line 9 def negate @negate end |
Instance Method Details
#because(message) ⇒ Object Also known as: explain
Change the message given when this validation fails.
18 19 20 21 |
# File 'lib/exocora/validation.rb', line 18 def because() @message = self end |
#if(filter, &block) ⇒ Object Also known as: with, when
Sets the filter used
66 67 68 69 70 |
# File 'lib/exocora/validation.rb', line 66 def if(filter, &block) @filter = block_given? ? block : filter @negate = false self end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/exocora/validation.rb', line 24 def to_s "#{@message} #{@negate ? 'unless' : 'when'} #{@filter.inspect}" end |
#unless(filter, &block) ⇒ Object
Adds the opposite of this filter
29 30 31 32 33 |
# File 'lib/exocora/validation.rb', line 29 def unless(filter, &block) @filter = block_given? ? block : filter @negate = true self end |
#validate(value) ⇒ Object
Applies this validation to a valie. Returns true, or raises a ValidationError.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/exocora/validation.rb', line 37 def validate(value) # Treat the filter as a function or a rubric if @filter.respond_to? :call method = :call else method = :=== end # Check the value begin result = @filter.send method, value rescue result = false end # If we're a negative filter, negate the result if negate result = ! result end # Return if result value else raise ValidationError.new(value, @message) end end |