Same custom method validation with different fields of same type

How can I refactor this coordinate validation routine so that I can use this method with other fields of the same type, for instance, with "full_last_position" and also maintain the cleanliness of the code and avoiding repeating method code?

class Provider < ActiveRecord::Base   include UtilsModule   attr_accessor :full_target_area_coordinate, :full_last_position # this field validates and saves coordinate

  validate :full_target_area_coordinate, :is_valid_coordinate?

  def is_valid_coordinate? # searches local geo_codes table for valid zip     coord = self.full_target_area_coordinate.to_str     match = coord.match(/^(-?\d{1,2}\.\d{6}),\s*(-?\d{1,2}\.\d{6})$/)     if match.nil? # invalid format       errors.add(:target_area_coordinate, :invalid)       return false     end

    self.target_area_coordinate = UtilsModule.convert_coordinate(self.full_target_area_coordinate)     return true   end

end

  def is_valid_coordinate? # searches local geo_codes table for valid zip     coord = self.full_target_area_coordinate.to_str

    coord = self.to_str

    match = coord.match(/^(-?\d{1,2}\.\d{6}),\s*(-?\d{1,2}\.\d{6})$/)     if match.nil? # invalid format       errors.add(:target_area_coordinate, :invalid)

errors.add(self.to_sym, :invalid)

      return false     end

    self.target_area_coordinate = UtilsModule.convert_coordinate(self.full_target_area_coordinate)

just return true here. i would suggest separating the conversion from the validation.

kind regards, --botp

How can I refactor this coordinate validation routine so that I can use this method with other fields of the same type, for instance, with "full_last_position" and also maintain the cleanliness of the code and avoiding repeating method code?

Maybe what you're looking for is ActiveModel::Validator ?