Module: Waw::Validation::Helpers

Included in:
Waw::Validation, Validator
Defined in:
lib/waw/validation/helpers.rb

Instance Method Summary collapse

Instance Method Details

#all_missing?(values) ⇒ Boolean

Checks if all values are missing

Returns:



26
27
28
# File 'lib/waw/validation/helpers.rb', line 26

def all_missing?(values)
  values.all?{|v| is_missing?(v)}
end

#any_missing?(values) ⇒ Boolean

Checks if any value is missing

Returns:



31
32
33
# File 'lib/waw/validation/helpers.rb', line 31

def any_missing?(values)
  values.any?{|v| is_missing?(v)}
end

#argument_safeObject

Yields the block inside a rescue on ArgumentError. Returns false if such an error has been raised



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

def argument_safe
  yield
rescue ArgumentError
  false
end

#error(msg, the_caller) ⇒ Object

Sends a friendly error message if something is wrong

Raises:



6
7
8
# File 'lib/waw/validation/helpers.rb', line 6

def error(msg, the_caller)
  raise ::Waw::WawError, msg, the_caller
end

#is_missing?(value) ⇒ Boolean

Checks if a given value is considered missing. In a web environment, we consider a value being missing when it is nil or an empty string (when stripped). All validators should use this method to share a common definition for ‘missing’. It may be overriden, at your own risks however. This method always returns false when invoked on any value other than nil or a String.

Returns:



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

def is_missing?(value)
  value.nil? or (String===value and value.strip.empty?)
end

#missings_to_nil(values) ⇒ Object

Converts missing values to nil



21
22
23
# File 'lib/waw/validation/helpers.rb', line 21

def missings_to_nil(values)
  values.collect{|v| is_missing?(v) ? nil : v}
end

#no_missing?(values) ⇒ Boolean

Checks if no value is considered missing

Returns:



36
37
38
# File 'lib/waw/validation/helpers.rb', line 36

def no_missing?(values)
  not(any_missing?(values))
end

#to_validator(who) ⇒ Object

Automatically converts specials guys as validators (regular expressions, some ruby classes, etc.)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/waw/validation/helpers.rb', line 50

def to_validator(who)
  if who.is_a?(Class)
    Waw::Validation.validator_for_ruby_class(who, true)
  else
    case who
      when ::Waw::Validation::Validator
        who
      when Regexp
        RegexpValidator.new(who, true)
      else
        raise "Unable to convert #{who}:#{who.class} to a validator"
    end
  end
end