Sha256: bf5d26fa14419f1853a7350c379c2370eedbb18fbf40d2875bd591974ade1d7f

Contents?: true

Size: 984 Bytes

Versions: 1

Compression:

Stored size: 984 Bytes

Contents

module DataMapper
  module Validations
    
    class RequiredFieldValidator < GenericValidator

      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
        
        error_message = "%s must not be blank".t(Inflector.humanize(@field_name))
        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

1 entries across 1 versions & 1 rubygems

Version Path
datamapper-0.2.3 lib/data_mapper/validations/required_field_validator.rb