Sha256: 95fd5854870991d8c10657e91f1439636176b33c0099dc50c31fca0f36d03379

Contents?: true

Size: 759 Bytes

Versions: 10

Compression:

Stored size: 759 Bytes

Contents

# frozen_string_literal: true

class CoordinateValidator < BaseValidator

  BOUNDARIES ||= {
    latitude: 90.0,
    longitude: 180.0
  }.freeze

  def validate_each(record, attribute, value)
    assert_valid_boundary!
    super
  end

  private

  def assert_valid_boundary!
    assert_valid_option!(:boundary, BOUNDARIES.keys.push(:pair))
  end

  def boundary
    options[:boundary] || :pair
  end

  def valid_attr?
    case boundary
    when :latitude then valid_boundary?(:latitude)
    when :longitude then valid_boundary?(:longitude)
    else valid_boundary?(:latitude, value.first) && valid_boundary?(:longitude, value.last)
    end
  end

  def valid_boundary?(key, coordinate = nil)
    (coordinate || value).to_f.abs <= BOUNDARIES[key]
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lite-validators-1.1.1 lib/lite/validators/coordinate_validator.rb
lite-validators-1.1.0 lib/lite/validators/coordinate_validator.rb
lite-validators-1.0.7 lib/lite/validators/coordinate_validator.rb
lite-validators-1.0.6 lib/lite/validators/coordinate_validator.rb
lite-validators-1.0.5 lib/lite/validators/coordinate_validator.rb
lite-validators-1.0.4 lib/lite/validators/coordinate_validator.rb
lite-validators-1.0.3 lib/lite/validators/coordinate_validator.rb
lite-validators-1.0.2 lib/lite/validators/coordinate_validator.rb
lite-validators-1.0.1 lib/lite/validators/coordinate_validator.rb
lite-validators-1.0.0 lib/lite/validators/coordinate_validator.rb