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

- (Namespace) initialize(uri, prefix)

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

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

- (URIRef) method_missing(methodname, *args)

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"

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

- (Object) fragment

Returns the value of attribute fragment



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

def fragment
  @fragment
end

- (Object) prefix

Returns the value of attribute prefix



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

def prefix
  @prefix
end

Instance Method Details

- (URIRef) +(suffix)

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



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

- (Namespace) bind(graph)

Bind this namespace to a Graph



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

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

- (Boolean) eql?(other) Also known as: ==

Compare namespaces



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

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

- (Object) inspect



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

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

- (String) to_s



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

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

- (URIRef) uri

Make sure to attach fragment



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

def uri
  self + ""
end

- (String) xmlns_attr

Output xmlns attribute name



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

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

- (Hash{String => String}) xmlns_hash

Output namespace definition as a hash



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

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