Created
August 6, 2014 11:30
-
-
Save raroni/99ddb8eebf639096c899 to your computer and use it in GitHub Desktop.
Validation spike 2
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
class RidesIndexParamValidation | |
MAX_LIMIT = 200 | |
attr_reader :result, :failure_details | |
def initialize(params) | |
@params = params | |
@result = :unspecified | |
end | |
def execute | |
[:check_limit_presence, :check_limit_is_positive_integer, :check_limit_range].each do |method_name| | |
send(method_name) | |
return unless failed? | |
end | |
@result = :pass | |
end | |
def failed? | |
if @result == :unspecified | |
nil | |
else | |
@result == :failure | |
end | |
end | |
private | |
def fail(reason, attributes = {}) | |
@failure_details = attributes.merge(reason: reason) | |
@result = :failure | |
end | |
def check_limit_range | |
if @params[:limit].to_i > MAX_LIMIT | |
fail(:limit_is_too_high, max: MAX_LIMIT) | |
end | |
end | |
def check_limit_presence | |
if @params[:limit].blank? | |
fail(:no_limit) | |
end | |
end | |
def check_limit_is_positive_integer | |
if !params[:limit].match(/\A[0-9]+\Z/) | |
fail(:invalid_limit) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment