Add top-level alias functions for constructors of the basic datatypes

This commit is contained in:
Marcel Otto 2018-04-07 22:59:03 +02:00
parent aba08fc03a
commit df05445733
5 changed files with 57 additions and 24 deletions

View file

@ -5,6 +5,17 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
[Keep a CHANGELOG](http://keepachangelog.com).
## Unreleased
### Added
- top-level alias functions for constructors of the basic datatypes
[Compare v0.4.1...HEAD](https://github.com/marcelotto/rdf-ex/compare/v0.4.1...HEAD)
## 0.4.1 - 2018-03-19
### Added

View file

@ -329,7 +329,7 @@ iex> RDF.literal(42, datatype: XSD.double).value
42.0
```
For all of these supported XSD datatypes there're `RDF.Datatype`s available that allow the creation of `RDF.Literal`s with the respective datatype:
For all of these supported XSD datatypes there're `RDF.Datatype`s available that allow the creation of `RDF.Literal`s with the respective datatype. Their `new` constructor function can be called also via the alias functions on the top-level `RDF` namespace.
```elixir
iex> RDF.Double.new("0042").value
@ -337,15 +337,18 @@ iex> RDF.Double.new("0042").value
iex> RDF.Double.new(42).value
42.0
iex> RDF.double(42).value
42.0
```
The `RDF.Literal.valid?/1` function checks if a given literal is valid according to the [XML schema datatype] specification.
```elixir
iex> RDF.Literal.valid? RDF.Integer.new("42")
iex> RDF.Literal.valid? RDF.integer("42")
true
iex> RDF.Literal.valid? RDF.Integer.new("foo")
iex> RDF.Literal.valid? RDF.integer("foo")
false
```
@ -354,31 +357,31 @@ If you want to prohibit the creation of invalid literals, you can use the `new!`
A RDF literal is bound to the lexical form of the initially given value. This lexical representation can be retrieved with the `RDF.Literal.lexical/1` function:
```elixir
iex> RDF.Literal.lexical RDF.Integer.new("0042")
iex> RDF.Literal.lexical RDF.integer("0042")
"0042"
iex> RDF.Literal.lexical RDF.Integer.new(42)
iex> RDF.Literal.lexical RDF.integer(42)
"42"
```
Although two literals might have the same value, they are not equal if they don't have the same lexical form:
```elixir
iex> RDF.Integer.new("0042").value == RDF.Integer.new("42").value
iex> RDF.integer("0042").value == RDF.integer("42").value
true
iex> RDF.Integer.new("0042") == RDF.Integer.new("42")
iex> RDF.integer("0042") == RDF.integer("42")
false
```
The `RDF.Literal.canonical/1` function returns the given literal with its canonical lexical form according its datatype:
```elixir
iex> RDF.Integer.new("0042") |> RDF.Literal.canonical |> RDF.Literal.lexical
iex> RDF.integer("0042") |> RDF.Literal.canonical |> RDF.Literal.lexical
"42"
iex> RDF.Literal.canonical(RDF.Integer.new("0042")) ==
RDF.Literal.canonical(RDF.Integer.new("42"))
iex> RDF.Literal.canonical(RDF.integer("0042")) ==
RDF.Literal.canonical(RDF.integer("42"))
true
```
@ -394,13 +397,13 @@ The `RDF.Triple` and `RDF.Quad` modules both provide a function `new` for such t
```elixir
iex> RDF.triple(EX.S, EX.p, 1)
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.Integer.new(1)}
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.integer(1)}
iex> RDF.triple {EX.S, EX.p, 1}
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.Integer.new(1)}
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.integer(1)}
iex> RDF.quad(EX.S, EX.p, 1, EX.Graph)
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.Integer.new(1),
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.integer(1),
~I<http://example.com/Graph>}
iex> RDF.triple {EX.S, 1, EX.O}
@ -413,7 +416,7 @@ If you want to explicitly create a quad in the default graph context, you can us
```elixir
iex> RDF.quad(EX.S, EX.p, 1, nil)
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.Integer.new(1), nil}
{~I<http://example.com/S>, ~I<http://example.com/p>, RDF.integer(1), nil}
```

View file

@ -1 +1 @@
0.4.1
0.4.2-dev

View file

@ -121,16 +121,26 @@ defmodule RDF do
defdelegate list?(resource, graph), to: RDF.List, as: :node?
defdelegate list?(description), to: RDF.List, as: :node?
def list(native_list),
do: RDF.List.from(native_list)
def list(head, %Graph{} = graph),
do: RDF.List.new(head, graph)
def list(native_list, opts),
do: RDF.List.from(native_list, opts)
def list(native_list), do: RDF.List.from(native_list)
def list(head, %Graph{} = graph), do: RDF.List.new(head, graph)
def list(native_list, opts), do: RDF.List.from(native_list, opts)
defdelegate string(value), to: RDF.String, as: :new
defdelegate string(value, opts), to: RDF.String, as: :new
defdelegate lang_string(value), to: RDF.LangString, as: :new
defdelegate lang_string(value, opts), to: RDF.LangString, as: :new
defdelegate boolean(value), to: RDF.Boolean, as: :new
defdelegate boolean(value, opts), to: RDF.Boolean, as: :new
defdelegate integer(value), to: RDF.Integer, as: :new
defdelegate integer(value, opts), to: RDF.Integer, as: :new
defdelegate double(value), to: RDF.Double, as: :new
defdelegate double(value, opts), to: RDF.Double, as: :new
defdelegate date(value), to: RDF.Date, as: :new
defdelegate date(value, opts), to: RDF.Date, as: :new
defdelegate time(value), to: RDF.Time, as: :new
defdelegate time(value, opts), to: RDF.Time, as: :new
defdelegate date_time(value), to: RDF.DateTime, as: :new
defdelegate date_time(value, opts), to: RDF.DateTime, as: :new
for term <- ~w[type subject predicate object first rest value]a do
defdelegate unquote(term)(), to: RDF.NS.RDF

View file

@ -2,4 +2,13 @@ defmodule RDFTest do
use RDF.Test.Case
doctest RDF
test "Datatype constructor alias functions" do
RDF.Datatype.modules
|> Enum.each(fn datatype ->
"rdf/" <> alias_name = datatype |> Macro.underscore
assert apply(RDF, String.to_atom(alias_name), [1]) == datatype.new(1)
end)
end
end