Added better documentation.
This commit is contained in:
parent
6d2965b549
commit
c0eb9ec41f
|
|
@ -3,3 +3,4 @@
|
||||||
/deps
|
/deps
|
||||||
erl_crash.dump
|
erl_crash.dump
|
||||||
*.ez
|
*.ez
|
||||||
|
docs
|
||||||
|
|
|
||||||
10
README.md
10
README.md
|
|
@ -13,6 +13,16 @@ defp deps do
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
2. Add the isn extension to your database
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
{:ok, pid} = Postgrex.Connection.start_link(
|
||||||
|
hostname: "localhost",
|
||||||
|
database: "isn_test"
|
||||||
|
)
|
||||||
|
Postgrex.Connection.query!(pid, "CREATE EXTENSION isn;", [])
|
||||||
|
```
|
||||||
|
|
||||||
2. Register the postgrex extension
|
2. Register the postgrex extension
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
|
|
|
||||||
70
lib/isn.ex
70
lib/isn.ex
|
|
@ -4,6 +4,41 @@ defmodule Isn do
|
||||||
@behaviour Postgrex.Extension
|
@behaviour Postgrex.Extension
|
||||||
@isn ~w(ean13 isbn13 ismn13 issn13 isbn ismn issn upc)
|
@isn ~w(ean13 isbn13 ismn13 issn13 isbn ismn issn upc)
|
||||||
|
|
||||||
|
@moduledoc """
|
||||||
|
A Postgrex.Extension enabling the use of postgresql data types from the isn
|
||||||
|
extension.
|
||||||
|
|
||||||
|
Add this module as an extension when establishing your Postgrex connection:
|
||||||
|
|
||||||
|
Postgrex.Connection.start_link(
|
||||||
|
database: "isn_test",
|
||||||
|
extensions: [{Isn, {}}])
|
||||||
|
|
||||||
|
Then you can do Ecto.Migrations like this:
|
||||||
|
|
||||||
|
defmodule MyApp.Repo.Migrations.CreateBook do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
create table(:books) do
|
||||||
|
add :isbn, :isbn13
|
||||||
|
# other fields
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
You can also define Ecto.Models using the matching custom Ecto.Types:
|
||||||
|
|
||||||
|
defmodule MyApp.Book do
|
||||||
|
use MyApp.Web, :model
|
||||||
|
|
||||||
|
schema "books" do
|
||||||
|
field :isbn, Isn.ISBN13
|
||||||
|
# other fields
|
||||||
|
end
|
||||||
|
end
|
||||||
|
"""
|
||||||
|
|
||||||
def init(parameters, _opts),
|
def init(parameters, _opts),
|
||||||
do: parameters
|
do: parameters
|
||||||
|
|
||||||
|
|
@ -21,13 +56,40 @@ defmodule Isn do
|
||||||
end
|
end
|
||||||
|
|
||||||
defmodule Isn.Base do
|
defmodule Isn.Base do
|
||||||
|
@moduledoc """
|
||||||
|
Base module for Isn custom ecto types.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Set up basic functionality for an Isn type.
|
||||||
|
|
||||||
|
This extends the calling module by defining implementations for
|
||||||
|
|
||||||
|
* type/0
|
||||||
|
* blank?/0
|
||||||
|
* cast/1
|
||||||
|
* load/1
|
||||||
|
* dump/1
|
||||||
|
"""
|
||||||
defmacro __using__(isn_type) do
|
defmacro __using__(isn_type) do
|
||||||
quote bind_quoted: [isn_type: isn_type] do
|
ecto_type = isn_type |> Atom.to_string |> String.upcase
|
||||||
# @behaviour Ecto.Type
|
quote bind_quoted: [isn_type: isn_type, ecto_type: ecto_type] do
|
||||||
#
|
@behaviour Ecto.Type
|
||||||
@isn_type isn_type
|
@isn_type isn_type
|
||||||
|
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Definition for the #{@isn_type} module.
|
Definition for the #{isn_type} module.
|
||||||
|
|
||||||
|
How to use this in an Ecto.Model
|
||||||
|
|
||||||
|
defmodule MyApp.Book do
|
||||||
|
use MyApp.Web, :model
|
||||||
|
|
||||||
|
schema "books" do
|
||||||
|
field :#{isn_type}, Isn.#{ecto_type}
|
||||||
|
# other fields
|
||||||
|
end
|
||||||
|
end
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def type,
|
def type,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue