Class: DataMapper::Validate::CustomValidator
- Inherits:
-
GenericValidator
- Object
- GenericValidator
- DataMapper::Validate::CustomValidator
- Defined in:
- lib/gems/dm-validations-0.9.9/lib/dm-validations/custom_validator.rb
Overview
Instance Attribute Summary
Attributes inherited from GenericValidator
Instance Method Summary collapse
- #call(target) ⇒ Object
-
#initialize(field_name, options = {}, &b) ⇒ CustomValidator
constructor
A new instance of CustomValidator.
Methods inherited from GenericValidator
#==, #add_error, #execute?, #field_name
Constructor Details
#initialize(field_name, options = {}, &b) ⇒ CustomValidator
Returns a new instance of CustomValidator.
12 13 14 15 16 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/custom_validator.rb', line 12 def initialize(field_name, = {}, &b) #super(field_name, options) #@field_name, @options = field_name, options #@options[:allow_nil] = false unless @options.has_key?(:allow_nil) end |
Instance Method Details
#call(target) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/custom_validator.rb', line 18 def call(target) #field_value = target.instance_variable_get("@#{@field_name}") #return true if @options[:allow_nil] && field_value.nil? #validation = (@options[:as] || @options[:with]) #error_message = nil # Figure out what to use as the actual validator. # If a symbol is passed to :as, look up # the canned validation in FORMATS. # #validator = if validation.is_a? Symbol # if FORMATS[validation].is_a? Array # error_message = FORMATS[validation][1] # FORMATS[validation][0] # else # FORMATS[validation] || validation # end #else # validation #end #valid = case validator #when Proc then validator.call(field_value) #when Regexp then validator =~ field_value #else raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{@field_name} with #{validator.inspect}" #end #unless valid # field = Inflector.humanize(@field_name) # value = field_value # # error_message = @options[:message] || error_message || '%s is invalid'.t(field) # error_message = error_message.call(field, value) if Proc === error_message # # add_error(target, error_message , @field_name) #end #return valid end |