Browse Source

chisel commit

jordan 3 years ago
parent
commit
6105f792d4

+ 38 - 0
iexnotes

@@ -0,0 +1,38 @@
+iex(1)> {:ok, pid} = ABCD.Fourletters.start_link([])
+{:ok, #PID<0.350.0>}
+iex(2)> ABCD.Fourletters.get(pid)
+[]
+iex(3)> ABCD.Fourletters.put(pid, "he")
+:ok
+iex(4)> ABCD.Fourletters.get(pid)
+["he"]
+iex(5)> ABCD.Fourletters.put(pid, "he")
+:ok
+iex(6)> ABCD.Fourletters.get(pid)
+["he", "he"]
+iex(7)> [%{id: :aoeu, start: {ABCD.Fourletters, :start_link, [[]]}}]
+[%{id: :aoeu, start: {ABCD.Fourletters, :start_link, [[]]}}]
+iex(8)> # iex(2)> {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
+nil
+iex(9)> children = [%{id: :aoeu, start: {ABCD.Fourletters, :start_link, [[]]}}]
+[%{id: :aoeu, start: {ABCD.Fourletters, :start_link, [[]]}}]
+iex(10)> {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
+{:ok, #PID<0.361.0>}
+
+
+iex(2)> {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
+{:ok, #PID<0.366.0>}
+iex(3)> Supervisor.count_children(pid)
+%{active: 1, specs: 1, supervisors: 0, workers: 1}
+iex(4)> Supervisor.start_child(pid,
+...(4)> %{id: :aoeu, start: {ABCD.Fourletters, :start_link, [[]]}})
+{:error, {:already_started, #PID<0.367.0>}}
+iex(5)> %{id: :tutu, start: {ABCD.Fourletters, :start_link, [[]]}})
+** (SyntaxError) iex:5:59: unexpected token: )
+
+iex(5)> Supervisor.start_child(pid, %{id: :tutu, start: {ABCD.Fourletters, :start_link, [[]]}})
+{:ok, #PID<0.373.0>}
+iex(6)> Supervisor.count_children(pid)
+%{active: 2, specs: 2, supervisors: 0, workers: 2}
+iex(7)> {:error, {:already_started, child_pid}} = Supervisor.start_child(pid, %{id: :tutu, start: {ABCD.Fourletters, :start_link, [[]]}})
+{:error, {:already_started, #PID<0.373.0>}}

+ 33 - 0
lib/abcd.ex

@@ -0,0 +1,33 @@
+defmodule ABCD.Fourletters do
+  use Agent
+
+  @doc """
+  Start a new fourletters.
+  """
+  def start_link(_opts) do
+    Agent.start_link(fn -> [] end)
+  end
+
+  @doc """
+  Get the posts on this fourletters. 
+  """
+  def get(name) do
+    Agent.get(name, & &1)
+  end
+
+  @doc """
+  Put a new post on these fourletters.
+  """
+  def put(name, value) do
+    Agent.update(name, fn list -> append(list, value) end)
+  end
+
+  defp append(lines, value) when is_list(lines) and length(lines) < 101 do
+    lines ++ [value]
+  end
+
+  defp append(lines, _values) do
+    lines
+  end
+end
+

+ 8 - 8
lib/fourletters_web/controllers/fourletters_controller.ex

@@ -1,17 +1,17 @@
 defmodule FourlettersWeb.FourlettersController do
   use FourlettersWeb, :controller
 
-  def index(conn, _params) do
-    render(conn, "index.html")
+  def fourletters(conn, %{"messenger" => messenger}) do
+    if String.length(messenger) == 4 do
+      render(conn, "four.html", messenger: messenger)
+    end
+    redirect(conn, to: "/")
   end
 
-  def show(conn, %{"messenger" => messenger}) do
-    render(conn, "show.html", messenger: messenger)
-  end
+  # def fourletters(conn, %{"messenger" => messenger}) do
+  #   render(conn, "nothing.html")
+  # end
 
-  def yo(conn, %{"messenger" => messenger}) do
-    render(conn, "yo.html", messenger: messenger)
-  end
   def nothing(conn, _params) do
     render(conn, "nothing.html")
   end

+ 3 - 3
lib/fourletters_web/router.ex

@@ -17,9 +17,9 @@ defmodule FourlettersWeb.Router do
     pipe_through :browser
 
     get "/", FourlettersController, :nothing
-    get "/:messenger", FourlettersController, :yo
-    get "/fourletters", FourlettersController, :index
-    get "/fourletters/:messenger", FourlettersController, :show
+    get "/:messenger", FourlettersController, :fourletters
+    # get "/fourletters", FourlettersController, :index
+    # get "/fourletters/:messenger", FourlettersController, :show
   end
 
   # Other scopes may use custom stacks.

+ 0 - 0
lib/fourletters_web/templates/fourletters/yo.html.eex → lib/fourletters_web/templates/fourletters/four.html.eex


+ 1 - 1
lib/fourletters_web/templates/fourletters/nothing.html.eex

@@ -1 +1 @@
-<h1>nothing</h1>
+<h1>4-letters.net</h1>