Class: Waw::Validation::DefaultValidator

Inherits:
Validator show all
Defined in:
lib/waw/validation/default_validator.rb

Instance Method Summary collapse

Methods inherited from Validator

#&, #===, #=~, #not, #|

Methods included from Helpers

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

Constructor Details

#initialize(*args, &block) ⇒ DefaultValidator

Creates a validator instance



6
7
8
9
10
11
# File 'lib/waw/validation/default_validator.rb', line 6

def initialize(*args, &block)
  error("Validator default expects one argument or a block: default(19) or default{Time.now} for example", caller)\
    unless (args.size==1 and block.nil?) or (args.empty? and not(block.nil?))
  @which = args.empty? ? :block : :arg
  @default_value = args.empty? ? block : args[0]
end

Instance Method Details

#compute_default(value) ⇒ Object

Computes the default value from a missing one



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

def compute_default(value)
  @which == :block ? @default_value.call(value) : @default_value
end

#convert_and_validate(*values) ⇒ Object

Converts missing values by the default one



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

def convert_and_validate(*values)
  validate(*values) ? [true, values.collect{|v| compute_default(v)}] : [false, values]
end

#validate(*values) ⇒ Object

Always returns true



14
15
16
# File 'lib/waw/validation/default_validator.rb', line 14

def validate(*values)
  all_missing?(values)
end