Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. defmodule Resource do
  2. def not_fetched, do: %{ state: :notFetched }
  3.  
  4. def loading, do: %{ state: :loading }
  5.  
  6. def loaded(val), do: %{ state: :loaded, value: val }
  7.  
  8. def error(err), do: %{ state: :error, error: err }
  9. end
  10.  
  11. defmodule Question do
  12. def new presentation, text do
  13. %{ presentation: presentation, asked: "just now", text: text }
  14. end
  15.  
  16. def view question do
  17. """
  18. Asked ${ question.asked }
  19.  
  20. ${ question.text }
  21. """
  22. end
  23. end
  24.  
  25. defmodule App do
  26. def init do
  27. %{ questions: Resource.not_fetched }
  28. end
  29.  
  30. def view %{ :questions => resource } do
  31. case resource do
  32. Resource.not_fetched ->
  33. "Please wait while we connect to the application API."
  34.  
  35. Resource.loading ->
  36. "Loading..."
  37.  
  38. Resource.loaded questions ->
  39. questions
  40. |> Enum.map( &Question.view/1 )
  41. |> Enum.join( "\n" )
  42.  
  43. Resource.error err ->
  44. "ERROR: ${err}"
  45. end
  46. end
  47. end
  48.  
  49. App.init
  50. |> App.view
  51. |> IO.puts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement