Sha256: f574b2176cdd1614112f49f06786e45262f78316f437d749f7e802e8027b77c0

Contents?: true

Size: 938 Bytes

Versions: 5

Compression:

Stored size: 938 Bytes

Contents

# Allows to check if the value of a specific attribute is a valid MAC address.
#
# @example Validate that the device MAC address is valid.
#   class Device << ActiveRecord::Base
#     attr_accessor :lon
#     validates :lon, longitude: true
#   end
class LongitudeValidator < ActiveModel::EachValidator
  # Checks if an attribute value is a valid MAC address.
  #
  # @param [Object] record object to validate
  # @param [String] attribute name of the object attribute to validate
  # @param [Object] value attribute value
  def validate_each(record, attribute, value)
    allow_blank = options[:allow_blank] || false
    return if allow_blank && value.blank?

    unless self.class.valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('errors.messages.longitude'))
    end
  end

  private

  def self.valid?(longitude, options)
    longitude.present? && longitude >= -180 && longitude <= 180
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
missing_validators-1.0.1 lib/missing_validators/validators/longitude_validator.rb
missing_validators-0.9.1 lib/missing_validators/validators/longitude_validator.rb
missing_validators-0.9 lib/missing_validators/validators/longitude_validator.rb
missing_validators-0.8.3 lib/missing_validators/validators/longitude_validator.rb
missing_validators-0.8.2 lib/missing_validators/validators/longitude_validator.rb