Last active
August 29, 2015 14:24
-
-
Save pumpkincouture/9e54626986d84807ac98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #All our fields require presence, so we create a base class | |
| class BaseValidation | |
| def absent?(value) | |
| value.nil? || value.empty? | |
| end | |
| end | |
| class DateValidation < BaseValidation | |
| def check_for_errors(value) | |
| if absent?(value) || invalid_format?(value) || invalid_date?(value) | |
| 'Invalid Date: Be sure to use mm/dd/yyyy format.' | |
| end | |
| end | |
| def invalid_format?(value) | |
| !/\d\d\/\d\d\/\d\d\d\d/.match(value) | |
| end | |
| def invalid_date?(value) | |
| Date.today <= Date.strptime(value, '%m/%d/%Y') | |
| end | |
| end | |
| class IncludedValidation < BaseValidation | |
| attr_reader :valid_values | |
| def initialize(valid_values) | |
| @valid_values = valid_values | |
| end | |
| def check_for_errors(value) | |
| if absent?(value) || !valid_values.include?(value) | |
| 'That is not a valid choice' | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment