class DefraRuby::Validators::GridReferenceValidator
Public Instance Methods
validate_each(record, attribute, value)
click to toggle source
# File lib/defra_ruby/validators/grid_reference_validator.rb, line 10 def validate_each(record, attribute, value) return false unless value_is_present?(record, attribute, value) return false unless valid_format?(record, attribute, value) valid_coordinate?(record, attribute, value) end
Private Instance Methods
five_digits()
click to toggle source
# File lib/defra_ruby/validators/grid_reference_validator.rb, line 45 def five_digits '\d{5}' end
grid_reference_pattern()
click to toggle source
# File lib/defra_ruby/validators/grid_reference_validator.rb, line 37 def grid_reference_pattern [two_letters, optional_space, five_digits, optional_space, five_digits].join end
optional_space()
click to toggle source
# File lib/defra_ruby/validators/grid_reference_validator.rb, line 49 def optional_space '\s*' end
two_letters()
click to toggle source
# File lib/defra_ruby/validators/grid_reference_validator.rb, line 41 def two_letters "[A-Za-z]{2}" end
valid_coordinate?(record, attribute, value)
click to toggle source
# File lib/defra_ruby/validators/grid_reference_validator.rb, line 29 def valid_coordinate?(record, attribute, value) OsMapRef::Location.for(value).easting true rescue OsMapRef::Error add_validation_error(record, attribute, :invalid) false end
valid_format?(record, attribute, value)
click to toggle source
Note that OsMapRef will work with less specific coordinates than are required for this service (100m) - so we need to add an additional check rather than just jump straight to confirming if it's a valid coordinate
# File lib/defra_ruby/validators/grid_reference_validator.rb, line 22 def valid_format?(record, attribute, value) return true if value.match?(/\A#{grid_reference_pattern}\z/) add_validation_error(record, attribute, :invalid_format) false end