Skip to content

Commit

Permalink
Simplify get/1 return
Browse files Browse the repository at this point in the history
  • Loading branch information
qcam committed May 16, 2020
1 parent dcb6e77 commit 6527ecd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 63 deletions.
6 changes: 4 additions & 2 deletions lib/nabo/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ defmodule Nabo.Post do
:reading_time
]

@type t :: %__MODULE__{
@type slug() :: String.t()

@type t() :: %__MODULE__{
body: String.t,
body_html: String.t,
datetime: DateTime.t,
Expand All @@ -68,7 +70,7 @@ defmodule Nabo.Post do
excerpt: String.t,
excerpt_html: String.t,
metadata: Map.t,
slug: String.t,
slug: slug(),
title: String.t,
}

Expand Down
28 changes: 13 additions & 15 deletions lib/nabo/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,19 @@ defmodule Nabo.Repo do

for %{slug: slug} = post <- @posts do
def get(unquote(slug)) do
{:ok, unquote(Macro.escape(post))}
unquote(Macro.escape(post))
end
end

def get(slug) do
{:error, "cannot find post #{slug}, availables: #{inspect(availables())}"}
end
def get(slug), do: nil

def get!(name) when is_binary(name) do
case get(name) do
{:ok, post} ->
post
def get!(slug) when is_binary(slug) do
case get(slug) do
nil ->
raise "could not find post with #{inspect(slug)}, availables: #{inspect(availables())}"

{:error, reason} ->
raise(reason)
post ->
post
end
end

Expand Down Expand Up @@ -144,20 +142,20 @@ defmodule Nabo.Repo do
## Example
{:ok, post} = MyRepo.get("my-slug")
MyRepo.get("my-slug")
"""
@callback get(name :: String.t()) :: {:ok, Nabo.Post.t()} | {:error, any}
@callback get(slug :: Nabo.Post.slug()) :: Nabo.Post.t() | nil

@doc """
Similar to `get/1` but raises error when no post was found.
Similar to `get/1` but raises error when no post is found.
## Example
post = MyRepo.get!("my-slug")
"""
@callback get!(name :: String.t()) :: Nabo.Post.t()
@callback get!(slug :: Nabo.Post.slug()) :: Nabo.Post.t()

@doc """
Fetches all available posts in the repo.
Expand Down Expand Up @@ -209,5 +207,5 @@ defmodule Nabo.Repo do
availables = MyRepo.availables()
"""
@callback availables() :: List.t()
@callback availables() :: [Nabo.Post.slug()]
end
10 changes: 5 additions & 5 deletions test/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Nabo.IntegrationTest do
alias Nabo.TestRepos

test "get/1 with a short markdown" do
assert {:ok, post} = TestRepos.Default.get("this-year-0202")
assert post = TestRepos.Default.get("this-year-0202")

assert post.title == "This year 02-02"
assert post.slug == "this-year-0202"
Expand All @@ -15,11 +15,11 @@ defmodule Nabo.IntegrationTest do
assert post.body == "### Welcome!\n\nThis is your first blog post built with *Nabo blog engine*\n"
assert post.body_html == "<h3>Welcome!</h3>\n<p>This is your first blog post built with <em>Nabo blog engine</em></p>\n"

assert {:error, _} = TestRepos.Default.get("something-that-does-not-exist")
assert TestRepos.Default.get("something-that-does-not-exist") == nil
end

test "get/1 with a long markdown" do
assert {:ok, post} = TestRepos.Default.get("this-year-0101")
assert post = TestRepos.Default.get("this-year-0101")

assert post.title == "This year 01-01"
assert post.slug == "this-year-0101"
Expand All @@ -28,11 +28,11 @@ defmodule Nabo.IntegrationTest do
assert post.excerpt == "This is the post for 01-01"
assert post.excerpt_html == "<p>This is the post for 01-01</p>\n"

assert {:error, _} = TestRepos.Default.get("something-that-does-not-exist")
assert TestRepos.Default.get("something-that-does-not-exist") == nil
end

test "get/1 with customized compiler" do
assert {:ok, post} = TestRepos.Customized.get("this-year-0303")
assert post = TestRepos.Customized.get("this-year-0303")

assert post.body == "### Welcome!\n\n```elixir\na = 1\n```\n"
assert post.body_html == "<h3>Welcome!</h3>\n<pre><code class=\"elixir nabo-elixir\">a = 1</code></pre>\n"
Expand Down
41 changes: 0 additions & 41 deletions test/nabo/repo_test.exs

This file was deleted.

0 comments on commit 6527ecd

Please sign in to comment.