From ba23be86bb1273beaae689baaffbe0f6bd3e3e81 Mon Sep 17 00:00:00 2001 From: Martin Frost Date: Wed, 27 May 2015 18:15:09 +0200 Subject: [PATCH] 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 --- lib/isn.ex | 55 +++++++++++++++++++ lib/isn/base.ex | 24 -------- lib/isn/extension.ex | 21 ------- lib/isn/isbn13.ex | 3 - mix.exs | 2 +- test/isn/ean13_test.exs | 19 +++++++ test/isn/isbn_test.exs | 19 +++++++ test/isn/ismn13_test.exs | 18 ++++++ test/isn/ismn_test.exs | 19 +++++++ test/isn/issn13_test.exs | 18 ++++++ test/isn/issn_test.exs | 19 +++++++ test/isn/upc_test.exs | 20 +++++++ test/{isn/extension_test.exs => isn_test.exs} | 4 +- 13 files changed, 190 insertions(+), 51 deletions(-) create mode 100644 lib/isn.ex delete mode 100644 lib/isn/base.ex delete mode 100644 lib/isn/extension.ex delete mode 100644 lib/isn/isbn13.ex rename test/{isn/extension_test.exs => isn_test.exs} (95%) diff --git a/lib/isn.ex b/lib/isn.ex new file mode 100644 index 0000000..471ffd0 --- /dev/null +++ b/lib/isn.ex @@ -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) diff --git a/lib/isn/base.ex b/lib/isn/base.ex deleted file mode 100644 index 6bdb63e..0000000 --- a/lib/isn/base.ex +++ /dev/null @@ -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 diff --git a/lib/isn/extension.ex b/lib/isn/extension.ex deleted file mode 100644 index d97142c..0000000 --- a/lib/isn/extension.ex +++ /dev/null @@ -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 diff --git a/lib/isn/isbn13.ex b/lib/isn/isbn13.ex deleted file mode 100644 index e860c40..0000000 --- a/lib/isn/isbn13.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule Isn.ISBN13 do - use Isn.Base, :isbn13 -end diff --git a/mix.exs b/mix.exs index 68de93f..70f039c 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Isn.Mixfile do use Mix.Project - @version "0.0.1" + @version "0.1.0" def project do [app: :isn, diff --git a/test/isn/ean13_test.exs b/test/isn/ean13_test.exs index e69de29..58e6e62 100644 --- a/test/isn/ean13_test.exs +++ b/test/isn/ean13_test.exs @@ -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 + diff --git a/test/isn/isbn_test.exs b/test/isn/isbn_test.exs index e69de29..3f0a110 100644 --- a/test/isn/isbn_test.exs +++ b/test/isn/isbn_test.exs @@ -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 + diff --git a/test/isn/ismn13_test.exs b/test/isn/ismn13_test.exs index e69de29..06fd1c3 100644 --- a/test/isn/ismn13_test.exs +++ b/test/isn/ismn13_test.exs @@ -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 diff --git a/test/isn/ismn_test.exs b/test/isn/ismn_test.exs index e69de29..8cbbd2a 100644 --- a/test/isn/ismn_test.exs +++ b/test/isn/ismn_test.exs @@ -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 + diff --git a/test/isn/issn13_test.exs b/test/isn/issn13_test.exs index e69de29..c5fafed 100644 --- a/test/isn/issn13_test.exs +++ b/test/isn/issn13_test.exs @@ -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 diff --git a/test/isn/issn_test.exs b/test/isn/issn_test.exs index e69de29..102e860 100644 --- a/test/isn/issn_test.exs +++ b/test/isn/issn_test.exs @@ -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 + diff --git a/test/isn/upc_test.exs b/test/isn/upc_test.exs index e69de29..065c043 100644 --- a/test/isn/upc_test.exs +++ b/test/isn/upc_test.exs @@ -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 + + diff --git a/test/isn/extension_test.exs b/test/isn_test.exs similarity index 95% rename from test/isn/extension_test.exs rename to test/isn_test.exs index 5fc7cd2..33051f0 100644 --- a/test/isn/extension_test.exs +++ b/test/isn_test.exs @@ -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