Class: Waw::Validation::Validator

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/waw/validation/validator.rb

Overview

A validator reusable class.

Instance Method Summary collapse

Methods included from Helpers

#all_missing?, #any_missing?, #argument_safe, #error, #is_missing?, #missings_to_nil, #no_missing?, #to_validator

Constructor Details

#initialize(&block) ⇒ Validator

Creates a validator instance that takes a block as validation code



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

def initialize(&block)
  @block = block
end

Instance Method Details

#&(validator) ⇒ Object

Creates a validator by conjunction



42
43
44
# File 'lib/waw/validation/validator.rb', line 42

def &(validator)
  AndValidator.new(self, Waw::Validation.to_validator(validator))
end

#===(*args) ⇒ Object



19
20
21
# File 'lib/waw/validation/validator.rb', line 19

def ===(*args)
  validate(*args)
end

#=~(*args) ⇒ Object



27
28
29
# File 'lib/waw/validation/validator.rb', line 27

def =~(*args)
  convert_and_validate(*args)
end

#convert_and_validate(*values) ⇒ Object

Converts and validate



24
25
26
# File 'lib/waw/validation/validator.rb', line 24

def convert_and_validate(*values)
  validate(*values) ? [true, values] : [false, values]
end

#notObject

Negates this validator



32
33
34
# File 'lib/waw/validation/validator.rb', line 32

def not
  NotValidator.new(self)
end

#validate(*values) ⇒ Object

Calls the block installed at initialization time



15
16
17
18
# File 'lib/waw/validation/validator.rb', line 15

def validate(*values)
  raise "Missing validation block on #{self}" unless @block
  @block.call(*values)
end

#|(validator) ⇒ Object

Creates a validator by disjunction



37
38
39
# File 'lib/waw/validation/validator.rb', line 37

def |(validator)
  OrValidator.new(self, Waw::Validation.to_validator(validator))
end