Class: RdfContext::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf_context/namespace.rb

Overview

From RdfContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, prefix) ⇒ Namespace

Creates a new namespace given a URI and the prefix.

nil is a valid prefix to specify the default namespace

Example

Namespace.new("http://xmlns.com/foaf/0.1/", "foaf") # => returns a new Foaf namespace

Parameters:

  • uri (#to_s)

    the URI of the namespace

  • prefix (#to_s)

    the prefix of the namespace

Raises:

  • (ParserException)

    Checks validity of the desired prefix and raises if it is incorrect.

Author:

  • Tom Morris, Pius Uzamere



19
20
21
22
23
24
25
26
# File 'lib/rdf_context/namespace.rb', line 19

def initialize(uri, prefix)
  prefix = prefix.to_s

  @uri = uri.to_s

  raise ParserException, "Invalid prefix '#{prefix}'" unless prefix_valid?(prefix)
  @prefix = prefix
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodname, *args) ⇒ URIRef

Allows the construction of arbitrary URIs on the namespace.

To avoid naming problems, a suffix may have an appended ‘_’, which will be removed when the URI is generated.

Examples:

foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf"); foaf.knows # => returns a new URIRef with URI "http://xmlns.com/foaf/0.1/knows"
foaf = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf", true); foaf.knows # => returns a new URIRef with URI "http://xmlns.com/foaf/0.1/#knows"

Parameters:

  • methodname (#to_s)

    to append to NS URI to create a new URI

  • args (Array)

    Ignored arguments

Returns:

  • (URIRef)

    The newly created URI.

Raises:

  • (Error)

    Checks validity of the desired prefix and raises if it is incorrect.

Author:

  • Tom Morris

  • Pius Uzamere



43
44
45
# File 'lib/rdf_context/namespace.rb', line 43

def method_missing(methodname, *args)
  self + methodname
end

Instance Attribute Details

#fragmentObject

Returns the value of attribute fragment.



4
5
6
# File 'lib/rdf_context/namespace.rb', line 4

def fragment
  @fragment
end

#prefixObject

Returns the value of attribute prefix.



4
5
6
# File 'lib/rdf_context/namespace.rb', line 4

def prefix
  @prefix
end

Instance Method Details

#+(suffix) ⇒ URIRef

Construct a URIRef from a namespace as in method_missing, but without method collision issues. Rules are somewhat different than for normal URI unions, as the raw URI is used as the source, not a normalized URI, and the result is not normalized

Parameters:

  • methodname (#to_s)

    to append to NS URI to create a new URI

Returns:

  • (URIRef)

    The newly created URI.



52
53
54
55
56
57
# File 'lib/rdf_context/namespace.rb', line 52

def +(suffix)
  prefix = @uri
  suffix = suffix.to_s.sub(/^\#/, "") if prefix.index("#")
  suffix = suffix.to_s.sub(/_$/, '')
  URIRef.intern(prefix + suffix.to_s, :normalize => false, :namespace => self)
end

#bind(graph) ⇒ Namespace

Bind this namespace to a Graph

Parameters:

Returns:



68
69
70
# File 'lib/rdf_context/namespace.rb', line 68

def bind(graph)
  graph.bind(self)
end

#eql?(other) ⇒ Boolean Also known as: ==

Compare namespaces

Parameters:

Returns:

  • (Boolean)


75
76
77
# File 'lib/rdf_context/namespace.rb', line 75

def eql?(other)
  self.uri == other.uri
end

#inspectObject



97
98
99
# File 'lib/rdf_context/namespace.rb', line 97

def inspect
  "Namespace[abbr='#{prefix}',uri='#{@uri}']"
end

#to_sString

Returns:



93
94
95
# File 'lib/rdf_context/namespace.rb', line 93

def to_s
  "#{prefix}: #{@uri}"
end

#uriURIRef

Make sure to attach fragment

Returns:

  • (URIRef)

    The newly created URI.



61
62
63
# File 'lib/rdf_context/namespace.rb', line 61

def uri
  self + ""
end

#xmlns_attrString

Output xmlns attribute name

Returns:



82
83
84
# File 'lib/rdf_context/namespace.rb', line 82

def xmlns_attr
  prefix.empty? ? "xmlns" : "xmlns:#{prefix}"
end

#xmlns_hashHash{String => String}

Output namespace definition as a hash

Returns:



88
89
90
# File 'lib/rdf_context/namespace.rb', line 88

def xmlns_hash
  {xmlns_attr => @uri.to_s}
end