Class: Waw::Validation::Signature::ValidationRule

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

Overview

A validation rule

Instance Method Summary collapse

Constructor Details

#initialize(args, validator, onfailure) ⇒ ValidationRule

Creates a Validation instance



42
43
44
45
46
# File 'lib/waw/validation/signature.rb', line 42

def initialize(args, validator, onfailure)
  @args = args
  @validator = validator
  @onfailure = onfailure
end

Instance Method Details

#collect_on_hash(hash) ⇒ Object

Collects the interesting parameters on a hash



49
50
51
# File 'lib/waw/validation/signature.rb', line 49

def collect_on_hash(hash)
  @args.collect{|arg| hash[arg]}
end

#complete_defaults(hash) ⇒ Object

Completes a hash of default values



77
78
79
80
# File 'lib/waw/validation/signature.rb', line 77

def complete_defaults(hash)
  ok, values = @validator.convert_and_validate(*collect_on_hash(hash))
  @args.each_with_index {|arg, i| hash[arg] = values[i]}
end

#convert_and_validate(hash) ⇒ Object

Validates and convert a hash. Values of the hash are replaced by their convertions if the validation passes. In this case, a nil value is returned. Otherwise, the on_failure is returned and the hash is not changed.



63
64
65
66
67
# File 'lib/waw/validation/signature.rb', line 63

def convert_and_validate(hash)
  ok, values = @validator.convert_and_validate(*collect_on_hash(hash))
  @args.each_with_index {|arg, i| hash[arg] = values[i]} if ok
  ok ? nil : @onfailure
end

#first_converted(hash) ⇒ Object

Applies the validation and conversion rule on a hash. Returns the first converted (or non converted if validation fails) value.



71
72
73
74
# File 'lib/waw/validation/signature.rb', line 71

def first_converted(hash)
  ok, values = @validator.convert_and_validate(*collect_on_hash(hash))
  values[0]
end

#validate(hash) ⇒ Object

Validates argument values given through a hash. Returns nil if everything is ok, the on_failure installed parameters otherwise.



55
56
57
# File 'lib/waw/validation/signature.rb', line 55

def validate(hash)
  @validator.validate(*collect_on_hash(hash)) ? nil : @onfailure
end