From 41c9c85cd6bf568d29a810302de7b643adaa1d16 Mon Sep 17 00:00:00 2001 From: Martin Frost Date: Tue, 5 May 2015 22:15:40 +0200 Subject: [PATCH] WIP --- .gitignore | 5 +++++ README.md | 4 ++++ config/config.exs | 26 +++++++++++++++++++++++++ lib/isn/base.ex | 20 ++++++++++++++++++++ lib/isn/extension.ex | 21 +++++++++++++++++++++ lib/isn/isbn13.ex | 3 +++ mix.exs | 45 ++++++++++++++++++++++++++++++++++++++++++++ mix.lock | 4 ++++ test/isn_test.exs | 8 ++++++++ test/test_helper.exs | 1 + 10 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config/config.exs create mode 100644 lib/isn/base.ex create mode 100644 lib/isn/extension.ex create mode 100644 lib/isn/isbn13.ex create mode 100644 mix.exs create mode 100644 mix.lock create mode 100644 test/isn_test.exs create mode 100644 test/test_helper.exs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..755b605 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/_build +/cover +/deps +erl_crash.dump +*.ez diff --git a/README.md b/README.md new file mode 100644 index 0000000..0b9448b --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +Isn +=== + +** TODO: Add description ** diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..136603b --- /dev/null +++ b/config/config.exs @@ -0,0 +1,26 @@ +# This file is responsible for configuring your application +# and its dependencies with the aid of the Mix.Config module. +use Mix.Config + +# This configuration is loaded before any dependency and is restricted +# to this project. If another project depends on this project, this +# file won't be loaded nor affect the parent project. For this reason, +# if you want to provide default values for your application for third- +# party users, it should be done in your mix.exs file. + +# Sample configuration: +# +# config :logger, +# level: :info +# +# config :logger, :console, +# format: "$date $time [$level] $metadata$message\n", +# metadata: [:user_id] + +# It is also possible to import configuration files, relative to this +# directory. For example, you can emulate configuration per environment +# by uncommenting the line below and defining dev.exs, test.exs and such. +# Configuration from the imported file will override the ones defined +# here (which is why it is important to import them last). +# +# import_config "#{Mix.env}.exs" diff --git a/lib/isn/base.ex b/lib/isn/base.ex new file mode 100644 index 0000000..e3ac20b --- /dev/null +++ b/lib/isn/base.ex @@ -0,0 +1,20 @@ +defmodule Isn.Base do + defmacro __using__(type) do + quote bind_quoted: [type: type] do + # @behaviour Ecto.Type + # + @moduledoc """ + Definition for the #{@type} module. + """ + + def type, + do: type + + defdelegate blank?, to: Ecto.Type + + 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 new file mode 100644 index 0000000..bb522ea --- /dev/null +++ b/lib/isn/extension.ex @@ -0,0 +1,21 @@ +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: :string + + 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 new file mode 100644 index 0000000..e860c40 --- /dev/null +++ b/lib/isn/isbn13.ex @@ -0,0 +1,3 @@ +defmodule Isn.ISBN13 do + use Isn.Base, :isbn13 +end diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..68de93f --- /dev/null +++ b/mix.exs @@ -0,0 +1,45 @@ +defmodule Isn.Mixfile do + use Mix.Project + + @version "0.0.1" + + def project do + [app: :isn, + version: @version, + elixir: "~> 1.0", + deps: deps, + test_paths: ["test"], + # Hex + description: description, + package: package, + # Docs + name: "Isn", + docs: [source_ref: "v#{@version}", + source_url: "https://github.com/Frost/isn"] + ] + end + + # Configuration for the OTP application + # + # Type `mix help compile.app` for more information + def application do + [] + end + + defp description do + """ + Ecto types for the postgreSQL isn extension. + """ + end + + defp package do + [contributors: ["Martin Frost"], + licences: ["MIT"], + links: %{"GitHub" => "https://github.com/Frost/isn"}] + end + + defp deps do + [{:postgrex, "~> 0.8.1"}, + {:ecto, "~> 0.11.0"}] + end +end diff --git a/mix.lock b/mix.lock new file mode 100644 index 0000000..230a3e8 --- /dev/null +++ b/mix.lock @@ -0,0 +1,4 @@ +%{"decimal": {:hex, :decimal, "1.1.0"}, + "ecto": {:hex, :ecto, "0.11.0"}, + "poolboy": {:hex, :poolboy, "1.5.1"}, + "postgrex": {:hex, :postgrex, "0.8.1"}} diff --git a/test/isn_test.exs b/test/isn_test.exs new file mode 100644 index 0000000..ad58314 --- /dev/null +++ b/test/isn_test.exs @@ -0,0 +1,8 @@ +defmodule IsnTest do + use ExUnit.Case + doctest Isn + + test "the truth" do + assert 1 + 1 == 2 + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()