Skip to content

Instantly share code, notes, and snippets.

@raroni
Created August 6, 2014 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raroni/99ddb8eebf639096c899 to your computer and use it in GitHub Desktop.
Save raroni/99ddb8eebf639096c899 to your computer and use it in GitHub Desktop.
Validation spike 2
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