jgv / area

Perform a variety of conversions between places and area codes or zip codes.
http://jonathanvingiano.com/area
MIT License
176 stars 58 forks source link

Have valid? method to test for valid inputs rather than raising exceptions. #14

Open wakproductions opened 11 years ago

wakproductions commented 11 years ago

Hi Jonathan! What a terrific and useful gem you have here! Nice work.

May I suggest that you implement a way to handle invalid data aside from raising an exception? For use cases involving user-generated input, it may be necessary to have some sort of validation test. Creating a rescue block for every usage makes for some unsightly code. It would be nice to be able to do something like:

postal_code="SE151AA" # a UK postal code if postal_code.valid_zip_code?

do one thing

else

do invalid zip code handling

end

Another problem with the way errors are currently handled is that you can pass it an invalid code and in some circumstances it will return nil. This inconsistency makes error handling more difficult:

"ABCDE".to_region => nil