v0.1: Flesh out library with all types in isn

Restructuring: Isn.Extension is now called Isn, which means that the postgrex
configuration line becomes a bit shorter now:

```elixir
{:ok, pid} = Postgrex.Connection.start_link(
  database: "isn_test",
  extensions: [{Isn, {}}])
```

instead of the previous version, with `extensions: [{Isn.Extension, {}}]`.

I also implement the entire library in the `lib/isn.ex` file, since it felt
pretty silly to have eight different one-line files under `lib/isn/` looking
like this: `defmodule Isn.ISBN, do: use(Isn.Base, :isbn)`

We now have the following types:

- ISBN
- ISBN13
- ISMN
- ISMN13
- ISSN
- ISSN13
- EAN13
- UPC
This commit is contained in:
Martin Frost 2015-05-27 18:15:09 +02:00
parent 0cfd44c402
commit ba23be86bb
13 changed files with 190 additions and 51 deletions

55
lib/isn.ex Normal file
View File

@ -0,0 +1,55 @@
defmodule Isn do
alias Postgrex.TypeInfo
@behaviour Postgrex.Extension
@isn ~w(ean13 isbn13 ismn13 issn13 isbn ismn issn upc)
def init(parameters, _opts),
do: parameters
def matching(_library),
do: Enum.reduce(@isn, [], fn(t, ack) -> ack ++ [type: t] end)
def format(_),
do: :text
def encode(%TypeInfo{type: type}, binary, _types, _opts) when type in @isn,
do: binary
def decode(%TypeInfo{type: type}, binary, _types, _opts) when type in @isn,
do: binary
end
defmodule Isn.Base do
defmacro __using__(isn_type) do
quote bind_quoted: [isn_type: isn_type] do
# @behaviour Ecto.Type
#
@isn_type isn_type
@moduledoc """
Definition for the #{@isn_type} module.
"""
def type,
do: @isn_type
defdelegate blank?, to: Ecto.Type
def cast(nil), do: :error
def cast(isn), do: {:ok, to_string(isn)}
def load(isn), do: {:ok, to_string(isn)}
def dump(isn), do: {:ok, to_string(isn)}
end
end
end
defmodule Isn.ISBN, do: use(Isn.Base, :isbn)
defmodule Isn.ISMN, do: use(Isn.Base, :ismn)
defmodule Isn.ISSN, do: use(Isn.Base, :issn)
defmodule Isn.ISBN13, do: use(Isn.Base, :isbn13)
defmodule Isn.ISMN13, do: use(Isn.Base, :ismn13)
defmodule Isn.ISSN13, do: use(Isn.Base, :issn13)
defmodule Isn.UPC, do: use(Isn.Base, :upc)
defmodule Isn.EAN13, do: use(Isn.Base, :ean13)

View File

@ -1,24 +0,0 @@
defmodule Isn.Base do
defmacro __using__(isn_type) do
quote bind_quoted: [isn_type: isn_type] do
# @behaviour Ecto.Type
#
@isn_type isn_type
@moduledoc """
Definition for the #{@isn_type} module.
"""
def type,
do: @isn_type
defdelegate blank?, to: Ecto.Type
def cast(nil), do: :error
def cast(isn), do: {:ok, to_string(isn)}
def load(isn), do: {:ok, to_string(isn)}
def dump(isn), do: {:ok, to_string(isn)}
end
end
end

View File

@ -1,21 +0,0 @@
defmodule Isn.Extension do
alias Postgrex.TypeInfo
@behaviour Postgrex.Extension
@isn ~w(ean13 isbn13 ismn13 issn13 isbn ismn issn upc)
def init(parameters, _opts),
do: parameters
def matching(_library),
do: Enum.reduce(@isn, [], fn(t, ack) -> ack ++ [type: t] end)
def format(_),
do: :text
def encode(%TypeInfo{type: type}, binary, _types, _opts) when type in @isn,
do: binary
def decode(%TypeInfo{type: type}, binary, _types, _opts) when type in @isn,
do: binary
end

View File

@ -1,3 +0,0 @@
defmodule Isn.ISBN13 do
use Isn.Base, :isbn13
end

View File

@ -1,7 +1,7 @@
defmodule Isn.Mixfile do
use Mix.Project
@version "0.0.1"
@version "0.1.0"
def project do
[app: :isn,

View File

@ -0,0 +1,19 @@
defmodule Isn.EAN13Test do
use ExUnit.Case, async: true
@test_ean "022-035648348-1"
test "cast" do
assert Isn.EAN13.cast(@test_ean) == {:ok, @test_ean}
assert Isn.EAN13.cast(nil) == :error
end
test "load" do
assert Isn.EAN13.load(@test_ean) == {:ok, @test_ean}
end
test "dump" do
assert Isn.EAN13.dump(@test_ean) == {:ok, @test_ean}
end
end

View File

@ -0,0 +1,19 @@
defmodule Isn.ISBNTest do
use ExUnit.Case, async: true
@test_isbn "1-937785-58-0"
test "cast" do
assert Isn.ISBN.cast(@test_isbn) == {:ok, @test_isbn}
assert Isn.ISBN.cast(nil) == :error
end
test "load" do
assert Isn.ISBN.load(@test_isbn) == {:ok, @test_isbn}
end
test "dump" do
assert Isn.ISBN.dump(@test_isbn) == {:ok, @test_isbn}
end
end

View File

@ -0,0 +1,18 @@
defmodule Isn.ISMN13Test do
use ExUnit.Case, async: true
@test_ismn "979-0-060-11561-5"
test "cast" do
assert Isn.ISMN13.cast(@test_ismn) == {:ok, @test_ismn}
assert Isn.ISMN13.cast(nil) == :error
end
test "load" do
assert Isn.ISMN13.load(@test_ismn) == {:ok, @test_ismn}
end
test "dump" do
assert Isn.ISMN13.dump(@test_ismn) == {:ok, @test_ismn}
end
end

View File

@ -0,0 +1,19 @@
defmodule Isn.ISMNTest do
use ExUnit.Case, async: true
@test_ismn "M-060-11561-5"
test "cast" do
assert Isn.ISMN.cast(@test_ismn) == {:ok, @test_ismn}
assert Isn.ISMN.cast(nil) == :error
end
test "load" do
assert Isn.ISMN.load(@test_ismn) == {:ok, @test_ismn}
end
test "dump" do
assert Isn.ISMN.dump(@test_ismn) == {:ok, @test_ismn}
end
end

View File

@ -0,0 +1,18 @@
defmodule Isn.ISSN13Test do
use ExUnit.Case, async: true
@test_issn "977-1436-452-00-8"
test "cast" do
assert Isn.ISSN13.cast(@test_issn) == {:ok, @test_issn}
assert Isn.ISSN13.cast(nil) == :error
end
test "load" do
assert Isn.ISSN13.load(@test_issn) == {:ok, @test_issn}
end
test "dump" do
assert Isn.ISSN13.dump(@test_issn) == {:ok, @test_issn}
end
end

View File

@ -0,0 +1,19 @@
defmodule Isn.ISSNTest do
use ExUnit.Case, async: true
@test_issn "1436-4522"
test "cast" do
assert Isn.ISSN.cast(@test_issn) == {:ok, @test_issn}
assert Isn.ISSN.cast(nil) == :error
end
test "load" do
assert Isn.ISSN.load(@test_issn) == {:ok, @test_issn}
end
test "dump" do
assert Isn.ISSN.dump(@test_issn) == {:ok, @test_issn}
end
end

View File

@ -0,0 +1,20 @@
defmodule Isn.UPCTest do
use ExUnit.Case, async: true
@test_upc "220356483481"
test "cast" do
assert Isn.UPC.cast(@test_upc) == {:ok, @test_upc}
assert Isn.UPC.cast(nil) == :error
end
test "load" do
assert Isn.UPC.load(@test_upc) == {:ok, @test_upc}
end
test "dump" do
assert Isn.UPC.dump(@test_upc) == {:ok, @test_upc}
end
end

View File

@ -1,4 +1,4 @@
defmodule IsnExtensionTest do
defmodule IsnTest do
use ExUnit.Case, async: true
import Isn.TestHelper
alias Postgrex.Connection, as: P
@ -6,7 +6,7 @@ defmodule IsnExtensionTest do
setup do
{:ok, pid} = P.start_link(
database: "isn_test",
extensions: [{Isn.Extension, {}}])
extensions: [{Isn, {}}])
{:ok, [pid: pid]}
end