rdf-ex/lib/rdf/xsd.ex
2020-05-15 17:13:31 +02:00

49 lines
1.6 KiB
Elixir

defmodule RDF.XSD do
@moduledoc """
An implementation of the XML Schema (XSD) datatype system for use within `RDF.Literal.Datatype` system.
see <https://www.w3.org/TR/xmlschema-2/>
"""
import RDF.Utils.Guards
alias __MODULE__
@facets [
XSD.Facets.MinInclusive,
XSD.Facets.MaxInclusive
]
@doc """
The list of all XSD facets.
"""
@spec facets() :: Enum.t()
def facets(), do: @facets
@facets_by_name Map.new(@facets, fn facet -> {facet.name(), facet} end)
def facet(name) when is_ordinary_atom(name), do: @facets_by_name[to_string(name)]
def facet(name), do: @facets_by_name[name]
@doc """
Returns if the given value is a `RDF.XSD.Datatype` struct or `RDF.Literal` with a `RDF.XSD.Datatype`.
"""
defdelegate datatype?(value), to: RDF.Literal.Datatype.Registry, as: :xsd_datatype?
for datatype <- RDF.Literal.Datatype.Registry.builtin_xsd_datatypes() do
defdelegate unquote(String.to_atom(datatype.name))(value), to: datatype, as: :new
defdelegate unquote(String.to_atom(datatype.name))(value, opts), to: datatype, as: :new
elixir_name = Macro.underscore(datatype.name)
unless datatype.name == elixir_name do
defdelegate unquote(String.to_atom(elixir_name))(value), to: datatype, as: :new
defdelegate unquote(String.to_atom(elixir_name))(value, opts), to: datatype, as: :new
end
end
defdelegate datetime(value), to: XSD.DateTime, as: :new
defdelegate datetime(value, opts), to: XSD.DateTime, as: :new
defdelegate unquote(true)(), to: XSD.Boolean.Value
defdelegate unquote(false)(), to: XSD.Boolean.Value
end