class CoordinateValidator

Public Instance Methods

validate_each(record, attribute, value) click to toggle source

rubocop:disable Metrics/LineLength

# File lib/active_validation/validators/coordinate_validator.rb, line 8
def validate_each(record, attribute, value)
  boundary = options[:boundary] || :coordinate
  unless BOUNDARIES.include?(boundary)
    raise ArgumentError,
          "Unknown boundary: #{boundary.inspect}. Valid boundaries are: #{BOUNDARIES.map(&:inspect).join(', ')}"
  end

  return if valid?(value, options)

  record.errors[attribute] <<
    (options[:message] || I18n.t("active_validation.errors.messages.coordinate.#{boundary}"))
end

Private Instance Methods

valid?(value, options) click to toggle source
# File lib/active_validation/validators/coordinate_validator.rb, line 48
def valid?(value, options)
  valid_length?(value) &&
    valid_boundary?(value, options)
end
valid_boundary?(value, options) click to toggle source
# File lib/active_validation/validators/coordinate_validator.rb, line 36
def valid_boundary?(value, options)
  case options[:boundary]
  when :latitude
    valid_latitude?(value)
  when :longitude
    valid_longitude?(value)
  else
    valid_latitude?(value.first) &&
      valid_longitude?(value.last)
  end
end
valid_latitude?(value) click to toggle source

rubocop:enable Metrics/LineLength

# File lib/active_validation/validators/coordinate_validator.rb, line 24
def valid_latitude?(value)
  value >= -90.0 && value <= 90.0
end
valid_length?(value) click to toggle source
# File lib/active_validation/validators/coordinate_validator.rb, line 28
def valid_length?(value)
  value.present?
end
valid_longitude?(value) click to toggle source
# File lib/active_validation/validators/coordinate_validator.rb, line 32
def valid_longitude?(value)
  value >= -180.0 && value <= 180.0
end