rdf-ex/lib/rdf/xsd.ex

50 lines
1.6 KiB
Elixir
Raw Normal View History

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/>
"""
2020-05-15 15:13:31 +00:00
import RDF.Utils.Guards
2020-05-15 15:13:31 +00:00
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)
2020-05-15 15:13:31 +00:00
def facet(name) when is_ordinary_atom(name), do: @facets_by_name[to_string(name)]
def facet(name), do: @facets_by_name[name]
@doc """
2020-05-15 15:13:31 +00:00
Returns if the given value is a `RDF.XSD.Datatype` struct or `RDF.Literal` with a `RDF.XSD.Datatype`.
"""
2020-05-15 15:13:31 +00:00
defdelegate datatype?(value), to: RDF.Literal.Datatype.Registry, as: :xsd_datatype?
2020-05-15 15:13:31 +00:00
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