Class: CouchRest::Validation::FormatValidator
- Inherits:
-
GenericValidator
- Object
- GenericValidator
- CouchRest::Validation::FormatValidator
- Defined in:
- lib/couchrest/validation/validators/format_validator.rb
Overview
Constant Summary collapse
- FORMATS =
{}
Constants included from CouchRest::Validation::Format::Url
CouchRest::Validation::Format::Url::Url
Constants included from CouchRest::Validation::Format::Email
CouchRest::Validation::Format::Email::EmailAddress
Instance Attribute Summary
Attributes inherited from GenericValidator
#field_name, #if_clause, #unless_clause
Instance Method Summary collapse
- #call(target) ⇒ Object
-
#initialize(field_name, options = {}, &b) ⇒ FormatValidator
constructor
A new instance of FormatValidator.
Methods included from CouchRest::Validation::Format::Url
Methods included from CouchRest::Validation::Format::Email
Methods inherited from GenericValidator
Constructor Details
#initialize(field_name, options = {}, &b) ⇒ FormatValidator
Returns a new instance of FormatValidator.
41 42 43 44 45 |
# File 'lib/couchrest/validation/validators/format_validator.rb', line 41 def initialize(field_name, = {}, &b) super(field_name, ) @field_name, @options = field_name, @options[:allow_nil] = false unless @options.has_key?(:allow_nil) end |
Instance Method Details
#call(target) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/couchrest/validation/validators/format_validator.rb', line 47 def call(target) value = target.validation_property_value(field_name) return true if @options[:allow_nil] && value.nil? validation = @options[:as] || @options[:with] raise "No such predefined format '#{validation}'" if validation.is_a?(Symbol) && !FORMATS.has_key?(validation) validator = validation.is_a?(Symbol) ? FORMATS[validation][0] : validation valid = case validator when Proc then validator.call(value) when Regexp then value =~ validator else raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{field_name} with #{validator.inspect}" end return true if valid = @options[:message] || ValidationErrors.(:invalid, field_name) field = field_name.to_s.humanize = .call(field, value) if .respond_to?(:call) add_error(target, , field_name) false end |