diff --git a/lib/rdf.ex b/lib/rdf.ex index c65d571..72ef425 100644 --- a/lib/rdf.ex +++ b/lib/rdf.ex @@ -1,5 +1,49 @@ defmodule RDF do - alias RDF.{Namespace, Literal, BlankNode, Triple, Quad} + alias RDF.{Namespace, Literal, BlankNode, Triple, Quad, + Description, Graph, Dataset} + + @doc """ + Checks if the given value is a RDF resource. + + ## Examples + + iex> RDF.resource?(RDF.uri("http://example.com/resource")) + true + iex> RDF.resource?(EX.resource) + true + iex> RDF.resource?(RDF.bnode) + true + iex> RDF.resource?(42) + false + """ + def resource?(value) + def resource?(%URI{}), do: true + def resource?(atom) when is_atom(atom), do: resource?(Namespace.resolve_term(atom)) + def resource?(%BlankNode{}), do: true + def resource?(_), do: false + + + @doc """ + Checks if the given value is an URI. + + ## Examples + + iex> RDF.uri?("http://www.example.com/foo") + true + iex> RDF.uri?("not a uri") + false + """ + def uri?(some_uri = %URI{}) do + # The following was suggested at http://stackoverflow.com/questions/30696761/check-if-a-url-is-valid-in-elixir + # TODO: Find a better way! Maybe ? + case some_uri do + %URI{scheme: nil} -> false + _uri -> true + end + end + def uri?(value) when is_binary(value), do: uri?(URI.parse(value)) + def uri?(_), do: false + @doc """ Generator function for URIs from strings or term atoms of a `RDF.Namespace`. @@ -24,113 +68,41 @@ defmodule RDF do def uri(atom) when is_atom(atom), do: Namespace.resolve_term(atom) def uri(string) do - parsed_uri = URI.parse(string) - if uri?(parsed_uri) do - parsed_uri - else - raise RDF.InvalidURIError, ~s(string "#{string}" is not a valid URI) + with parsed_uri = URI.parse(string) do + if uri?(parsed_uri) do + parsed_uri + else + raise RDF.InvalidURIError, ~s(string "#{string}" is not a valid URI) + end end end - @doc """ - Checks if the given value is an URI. - ## Examples + defdelegate bnode(), to: BlankNode, as: :new + defdelegate bnode(id), to: BlankNode, as: :new - iex> RDF.uri?("http://www.example.com/foo") - true - iex> RDF.uri?("not a uri") - false - """ - def uri?(some_uri = %URI{}) do - # The following was a suggested at http://stackoverflow.com/questions/30696761/check-if-a-url-is-valid-in-elixir - # TODO: Find a better way! Maybe https://github.com/marcelog/ex_rfc3986 ? - case some_uri do - %URI{scheme: nil} -> false - _uri -> true - end - end - def uri?(value) when is_binary(value), do: uri?(URI.parse(value)) - def uri?(_), do: false + defdelegate literal(value), to: Literal, as: :new + defdelegate literal(value, opts), to: Literal, as: :new + defdelegate triple(s, p, o), to: Triple, as: :new + defdelegate triple(tuple), to: Triple, as: :new - @doc """ - Generator function for `RDF.Literal` values. + defdelegate quad(s, p, o, g), to: Quad, as: :new + defdelegate quad(tuple), to: Quad, as: :new - ## Examples + defdelegate description(arg), to: Description, as: :new + defdelegate description(arg1, arg2), to: Description, as: :new + defdelegate description(arg1, arg2, arg3), to: Description, as: :new - iex> RDF.literal(42) - %RDF.Literal{value: 42, datatype: XSD.integer} - """ - def literal(value) + defdelegate graph(), to: Graph, as: :new + defdelegate graph(arg), to: Graph, as: :new + defdelegate graph(arg1, arg2), to: Graph, as: :new + defdelegate graph(arg1, arg2, arg3), to: Graph, as: :new + defdelegate graph(arg1, arg2, arg3, arg4), to: Graph, as: :new - def literal(lit = %Literal{}), do: lit - def literal(value), do: Literal.new(value) - def literal(value, opts), do: Literal.new(value, opts) - - @doc """ - Generator function for `RDF.Triple`s. - - ## Examples - - iex> RDF.triple("http://example.com/S", "http://example.com/p", 42) - {RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42)} - iex> RDF.triple(EX.S, EX.p, 42) - {RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42)} - """ - def triple(subject, predicate, object), do: Triple.new(subject, predicate, object) - def triple(tuple), do: Triple.new(tuple) - - @doc """ - Generator function for `RDF.quad`s. - - ## Examples - - iex> RDF.quad("http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph") - {RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42), RDF.uri("http://example.com/Graph")} - iex> RDF.quad(EX.S, EX.p, 42, EX.Graph) - {RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42), RDF.uri("http://example.com/Graph")} - """ - def quad(subject, predicate, object, graph_context), - do: Quad.new(subject, predicate, object, graph_context) - def quad(tuple), do: Quad.new(tuple) - - - @doc """ - Generator function for `RDF.BlankNode`s. - """ - def bnode, do: BlankNode.new - - @doc """ - Generator function for `RDF.BlankNode`s with a user-defined identity. - - ## Examples - - iex> RDF.bnode(:foo) - %RDF.BlankNode{id: "foo"} - """ - def bnode(id), do: BlankNode.new(id) - - - @doc """ - Checks if the given value is a RDF resource. - - ## Examples - - iex> RDF.resource?(RDF.uri("http://example.com/resource")) - true - iex> RDF.resource?(EX.resource) - true - iex> RDF.resource?(RDF.bnode) - true - iex> RDF.resource?(42) - false - """ - def resource?(value) - def resource?(%URI{}), do: true - def resource?(atom) when is_atom(atom), do: resource?(Namespace.resolve_term(atom)) - def resource?(%BlankNode{}), do: true - def resource?(_), do: false + defdelegate dataset(), to: Dataset, as: :new + defdelegate dataset(arg), to: Dataset, as: :new + defdelegate dataset(arg1, arg2), to: Dataset, as: :new for term <- ~w[type subject predicate object first rest value]a do diff --git a/lib/rdf/literal.ex b/lib/rdf/literal.ex index 3af1164..09c0785 100644 --- a/lib/rdf/literal.ex +++ b/lib/rdf/literal.ex @@ -42,6 +42,8 @@ defmodule RDF.Literal do """ def new(value) + def new(%RDF.Literal{} = literal), do: literal + def new(value) when is_binary(value), do: RDF.String.new(value) def new(value) when is_boolean(value), do: RDF.Boolean.new(value) def new(value) when is_integer(value), do: RDF.Integer.new(value) diff --git a/lib/rdf/quad.ex b/lib/rdf/quad.ex index 1e1da7f..7f7f2e6 100644 --- a/lib/rdf/quad.ex +++ b/lib/rdf/quad.ex @@ -6,7 +6,7 @@ defmodule RDF.Quad do RDF values for subject, predicate, object and a graph context. """ - alias RDF.{BlankNode, Statement} + alias RDF.Statement @doc """ Creates a `RDF.Quad` with proper RDF values. @@ -19,6 +19,8 @@ defmodule RDF.Quad do iex> RDF.Quad.new("http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph") {~I, ~I, RDF.literal(42), ~I} + iex> RDF.Quad.new(EX.S, EX.p, 42, EX.Graph) + {RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42), RDF.uri("http://example.com/Graph")} """ def new(subject, predicate, object, graph_context) do { @@ -40,6 +42,8 @@ defmodule RDF.Quad do iex> RDF.Quad.new {"http://example.com/S", "http://example.com/p", 42, "http://example.com/Graph"} {~I, ~I, RDF.literal(42), ~I} + iex> RDF.Quad.new {EX.S, EX.p, 42, EX.Graph} + {RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42), RDF.uri("http://example.com/Graph")} """ def new({subject, predicate, object, graph_context}), do: new(subject, predicate, object, graph_context) diff --git a/lib/rdf/triple.ex b/lib/rdf/triple.ex index 2522a6f..6766de1 100644 --- a/lib/rdf/triple.ex +++ b/lib/rdf/triple.ex @@ -6,7 +6,7 @@ defmodule RDF.Triple do RDF values for subject, predicate and object. """ - alias RDF.{BlankNode, Statement} + alias RDF.Statement @doc """ Creates a `RDF.Triple` with proper RDF values. @@ -19,6 +19,8 @@ defmodule RDF.Triple do iex> RDF.Triple.new("http://example.com/S", "http://example.com/p", 42) {~I, ~I, RDF.literal(42)} + iex> RDF.Triple.new(EX.S, EX.p, 42) + {RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42)} """ def new(subject, predicate, object) do { @@ -39,6 +41,8 @@ defmodule RDF.Triple do iex> RDF.Triple.new {"http://example.com/S", "http://example.com/p", 42} {~I, ~I, RDF.literal(42)} + iex> RDF.Triple.new {EX.S, EX.p, 42} + {RDF.uri("http://example.com/S"), RDF.uri("http://example.com/p"), RDF.literal(42)} """ def new({subject, predicate, object}), do: new(subject, predicate, object) diff --git a/lib/rdf/vocabulary_namespace.ex b/lib/rdf/vocabulary_namespace.ex index a42de61..157731f 100644 --- a/lib/rdf/vocabulary_namespace.ex +++ b/lib/rdf/vocabulary_namespace.ex @@ -412,7 +412,7 @@ defmodule RDF.Vocabulary.Namespace do IO.warn "capitalized alias '#{term}' for a property" end - defp case_violation_warning(:lowercased_alias, term, _, base_uri) do + defp case_violation_warning(:lowercased_alias, term, _, _) do IO.warn "lowercased alias '#{term}' for a non-property resource" end diff --git a/test/unit/quad_test.exs b/test/unit/quad_test.exs index f9d4848..0761ef0 100644 --- a/test/unit/quad_test.exs +++ b/test/unit/quad_test.exs @@ -1,7 +1,5 @@ defmodule RDF.QuadTest do - use ExUnit.Case - - import RDF.Sigils + use RDF.Test.Case doctest RDF.Quad end diff --git a/test/unit/rdf_test.exs b/test/unit/rdf_test.exs index 644498b..10064c2 100644 --- a/test/unit/rdf_test.exs +++ b/test/unit/rdf_test.exs @@ -1,11 +1,5 @@ defmodule RDFTest do - use ExUnit.Case - - use RDF.Vocabulary.Namespace - defvocab EX, base_uri: "http://example.com/", terms: [], strict: false - - alias RDF.NS.XSD + use RDF.Test.Case doctest RDF - end diff --git a/test/unit/triple_test.exs b/test/unit/triple_test.exs index cba8e53..6ff686e 100644 --- a/test/unit/triple_test.exs +++ b/test/unit/triple_test.exs @@ -1,8 +1,5 @@ defmodule RDF.TripleTest do - use ExUnit.Case - - import RDF.Sigils + use RDF.Test.Case doctest RDF.Triple - end