Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
anoskov committed Jun 28, 2016
0 parents commit e6d494a
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## build system files
_build/
deps/
doc/
erl_crash.dump
*.ez

## Benchmark results files
bench/snapshots/

## generic files
*~
*.DS_Store
*.swp
*.out
Icon?
Thumbs.db
56 changes: 56 additions & 0 deletions lib/uuid5.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule UUID5 do

@moduledoc """
An Ecto type for UUIDs v5 strings.
"""

@behaviour Ecto.Type

@doc """
The Ecto type.
"""
def type, do: :uuid5

@doc """
Casts to UUID.
"""
def cast(<< _::64, ?-, _::32, ?-, _::32, ?-, _::32, ?-, _::96 >> = u), do: {:ok, u}
def cast(_), do: :error

@doc """
Converts a string representing a UUID into a binary.
"""
def dump(uuid) do
try do
UUID.string_to_binary!(uuid)
catch
:error -> :error
else
binary ->
{:ok, %Ecto.Query.Tagged{type: :uuid5, value: binary}}
end
end

@doc """
Converts a binary UUID into a string.
"""
def load(<<_::128>> = uuid) do
{:ok, UUID.binary_to_string!(uuid)}
end
def load(<<_::64, ?-, _::32, ?-, _::32, ?-, _::32, ?-, _::96>> = string) do
raise "trying to load string UUID as Ecto.UUID: #{inspect string}. " <>
"Maybe you wanted to declare :uuid as your database field?"
end
def load(%Ecto.Query.Tagged{type: :uuid5, value: uuid}) do
{:ok, UUID.binary_to_string!(uuid)}
end
def load(_), do: :error

@doc """
Generates a version 5 (dns) UUID.
"""
def generate do
UUID.uuid5(:dns, UUID.uuid4)
end

end
44 changes: 44 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule UUID5.Mixfile do
use Mix.Project

@version "1.0.0"

def project do
[app: :uuid5,
name: "UUID5_Ecto_Type",
version: @version,
elixir: "~> 1.0",
docs: [],
source_url: "https://github.com/anoskov/uuid5_ecto_type",
description: description(),
package: package(),
deps: deps()]
end

# Application configuration.
def application do
[]
end

# List of dependencies.
defp deps do
[{:uuid, "~> 1.1"},
{:ecto, "~> 1.1.2"}]
end

# Description.
defp description do
"""
UUID v5 type for Ecto.
"""
end

# Package info.
defp package do
[files: ["lib", "mix.exs", "README.md", "LICENSE"],
maintainers: ["Andrey Noskov"],
licenses: ["Apache 2.0"],
links: %{"GitHub" => "https://github.com/anoskov/uuid5_ecto_type"}]
end

end
4 changes: 4 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%{"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []},
"ecto": {:hex, :ecto, "1.1.8", "0f0348e678fa5a450c266d69816808f97fbd82ade32cf88d4b09bbe8f8c27545", [:mix], [{:sbroker, "~> 0.7", [hex: :sbroker, optional: true]}, {:postgrex, "~> 0.11.0", [hex: :postgrex, optional: true]}, {:poolboy, "~> 1.4", [hex: :poolboy, optional: false]}, {:poison, "~> 1.0 or ~> 2.0", [hex: :poison, optional: true]}, {:mariaex, "~> 0.5.0 or ~> 0.6.0", [hex: :mariaex, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"uuid": {:hex, :uuid, "1.1.4", "36c7734e4c8e357f2f67ba57fb61799d60c20a7f817b104896cca64b857e3686", [:mix], []}}

0 comments on commit e6d494a

Please sign in to comment.