Class: Reddy::Namespace

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

Overview

From Reddy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, prefix, fragment = nil) ⇒ 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:: (String)

    the URI of the namespace

  • prefix:: (String)

    the prefix of the namespace

  • fragment:: (Boolean)

    are the identifiers on this resource fragment identifiers? (e.g. ‘#’) Defaults to false.

Raises:

  • (Error)

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

Author:

  • Tom Morris, Pius Uzamere



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/reddy/namespace.rb', line 20

def initialize(uri, prefix, fragment = nil)
  @uri = URIRef.new(uri) unless uri.is_a?(URIRef)
  @fragment = fragment
  @fragment = uri.to_s.match(/\#$/) ? true : false if fragment.nil?
  prefix = nil if prefix.to_s.empty?
  if prefix_valid?(prefix)
    @prefix = prefix
  else
    raise ParserException, "Invalid prefix '#{prefix}'"
  end
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.

Example

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"

Returns:

  • (URIRef)

    The newly created URIRegerence.

Raises:

  • (Error)

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

Author:

  • Tom Morris, Pius Uzamere



42
43
44
# File 'lib/reddy/namespace.rb', line 42

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

Instance Attribute Details

#fragmentObject

Returns the value of attribute fragment.



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

def fragment
  @fragment
end

#prefixObject

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

#uriObject

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#+(suffix) ⇒ Object

Construct a URIRef from a namespace as in method_missing, but without method collision issues



47
48
49
# File 'lib/reddy/namespace.rb', line 47

def +(suffix)
  URIRef.new((fragment ? "##{suffix}" : suffix.to_s), @uri)
end

#bind(graph) ⇒ Object

Bind this namespace to a Graph



52
53
54
# File 'lib/reddy/namespace.rb', line 52

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

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

Compare namespaces

Returns:

  • (Boolean)


57
58
59
# File 'lib/reddy/namespace.rb', line 57

def eql?(other)
  @prefix == other.prefix && @uri == other.uri && @fragment == other.fragment
end

#inspectObject



72
73
74
# File 'lib/reddy/namespace.rb', line 72

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

#xmlns_attrObject

Output xmlns attribute name



63
64
65
# File 'lib/reddy/namespace.rb', line 63

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

#xmlns_hashObject

Output namespace definition as a hash



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

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