Skip to content

Instantly share code, notes, and snippets.

@pumpkincouture
Last active August 29, 2015 14:24
Show Gist options
  • Save pumpkincouture/9e54626986d84807ac98 to your computer and use it in GitHub Desktop.
Save pumpkincouture/9e54626986d84807ac98 to your computer and use it in GitHub Desktop.
#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