Skip to content

Instantly share code, notes, and snippets.

@ijunaid8989
Created May 11, 2016 13:38
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 ijunaid8989/daee64d913588e10ba742a6c545f0222 to your computer and use it in GitHub Desktop.
Save ijunaid8989/daee64d913588e10ba742a6c545f0222 to your computer and use it in GitHub Desktop.
doing code with cond...
defp validate_params(params) do
cond do
!validate?(params["frequency"]) ->
{:invalid, "The parameter 'frequency' isn't valid."}
!validate?(params["storage_duration"]) ->
{:invalid, "The parameter 'storage_duration' isn't valid."}
!cr_status?(params["status"]) ->
{:invalid, "The parameter 'status' isn't valid."}
true ->
{:ok}
end
end
def validate?(nil), do: false
def validate?(integer) when is_integer(integer), do: true
def validate?(integer) do
case Integer.parse(integer) do
{_number, ""} -> true
_ -> false
end
end
def cr_status?(""), do: false
def cr_status?(nil), do: false
def cr_status?("off"), do: true
def cr_status?("on"), do: true
def cr_status?("on-scheduled"), do: true
def cr_status?(status), do: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment