Use __CALLER__ to determine isn datatype
`__CALLER__.module` contains enough information to determine what datatype to use, so the usage of `Isn.Base` can be simplified to the following ```elixir defmodule Isn.ISBN13 do use Isn.Base end ``` instead of ```elixir defmodule Isn.ISBN13 do use Isn.Base, :isbn13 end ```
This commit is contained in:
parent
f3514497fd
commit
15385bfc6b
27
lib/isn.ex
27
lib/isn.ex
|
|
@ -43,7 +43,7 @@ defmodule Isn do
|
||||||
do: parameters
|
do: parameters
|
||||||
|
|
||||||
def matching(_library),
|
def matching(_library),
|
||||||
do: Enum.reduce(@isn, [], fn(t, ack) -> ack ++ [type: t] end)
|
do: Enum.zip(Stream.cycle([:type]), @isn)
|
||||||
|
|
||||||
def format(_),
|
def format(_),
|
||||||
do: :text
|
do: :text
|
||||||
|
|
@ -71,8 +71,13 @@ defmodule Isn.Base do
|
||||||
* load/1
|
* load/1
|
||||||
* dump/1
|
* dump/1
|
||||||
"""
|
"""
|
||||||
defmacro __using__(isn_type) do
|
defmacro __using__(_opts) do
|
||||||
ecto_type = isn_type |> Atom.to_string |> String.upcase
|
ecto_type = __CALLER__.module
|
||||||
|
|> Atom.to_string
|
||||||
|
|> String.replace(~r(.+\.), "")
|
||||||
|
isn_type = ecto_type
|
||||||
|
|> String.downcase
|
||||||
|
|
||||||
quote bind_quoted: [isn_type: isn_type, ecto_type: ecto_type] do
|
quote bind_quoted: [isn_type: isn_type, ecto_type: ecto_type] do
|
||||||
@behaviour Ecto.Type
|
@behaviour Ecto.Type
|
||||||
@isn_type isn_type
|
@isn_type isn_type
|
||||||
|
|
@ -107,11 +112,11 @@ defmodule Isn.Base do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defmodule Isn.ISBN, do: use(Isn.Base, :isbn)
|
defmodule Isn.ISBN, do: use(Isn.Base)
|
||||||
defmodule Isn.ISMN, do: use(Isn.Base, :ismn)
|
defmodule Isn.ISMN, do: use(Isn.Base)
|
||||||
defmodule Isn.ISSN, do: use(Isn.Base, :issn)
|
defmodule Isn.ISSN, do: use(Isn.Base)
|
||||||
defmodule Isn.ISBN13, do: use(Isn.Base, :isbn13)
|
defmodule Isn.ISBN13, do: use(Isn.Base)
|
||||||
defmodule Isn.ISMN13, do: use(Isn.Base, :ismn13)
|
defmodule Isn.ISMN13, do: use(Isn.Base)
|
||||||
defmodule Isn.ISSN13, do: use(Isn.Base, :issn13)
|
defmodule Isn.ISSN13, do: use(Isn.Base)
|
||||||
defmodule Isn.UPC, do: use(Isn.Base, :upc)
|
defmodule Isn.UPC, do: use(Isn.Base)
|
||||||
defmodule Isn.EAN13, do: use(Isn.Base, :ean13)
|
defmodule Isn.EAN13, do: use(Isn.Base)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue