Sha256: d110145c848a3ab6125b4bfb4971ce3d343530c6319e80c33bf7982015fed8f5

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

module DataMapper
  module Validations
    
    class RequiredFieldValidator < GenericValidator
      
      ERROR_MESSAGES = {
        :required => '#{field} must not be blank'
      }
      
      def initialize(field_name)
        @field_name = field_name
      end
      
      def call(target)
        field_value = !target.instance_variable_get("@#{@field_name}").nil?
        return true if field_value
        
        field = Inflector.humanize(@field_name)
        
        error_message = validation_error_message(ERROR_MESSAGES[:required], nil, binding)        
        add_error(target, error_message , @field_name)
        
        return false
      end
      
    end
    
    module ValidatesPresenceOf
      def self.included(base)
        base.extend(ClassMethods)
      end
      
      module ClassMethods

        def validates_presence_of(*fields)
          options = retrieve_options_from_arguments_for_validators(fields)
          
          fields.each do |field|
            validations.context(options[:context]) << Validations::RequiredFieldValidator.new(field)
          end
        end

      end
    end
    
  end  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
datamapper-0.1.0 lib/data_mapper/validations/required_field_validator.rb
datamapper-0.1.1 lib/data_mapper/validations/required_field_validator.rb
datamapper-0.2.0 lib/data_mapper/validations/required_field_validator.rb
datamapper-0.2.1 lib/data_mapper/validations/required_field_validator.rb
datamapper-0.2.2 lib/data_mapper/validations/required_field_validator.rb