Class: RdfaParser::Namespace

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

Overview

From Reddy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, short, fragment = nil) ⇒ Namespace

Creates a new namespace given a URI and the short name.

Example

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

Returns

gk

nil is a valid shortname to specify the default namespace

Parameters:

  • uri (String)

    the URI of the namespace

  • short (String)

    the short name of the namespace

  • fragment (Boolean) (defaults to: nil)

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

Raises:

  • (Error)

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

Author:

  • Tom Morris, Pius Uzamere



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rdfa_parser/namespace.rb', line 23

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

Parameters:

  • uri (String)

    the URI of the namespace

  • short (String)

    the short name of the namespace

  • fragment (Boolean)

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

Returns:

  • (URIRef)

    The newly created URIRegerence.

Raises:

  • (Error)

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

Author:

  • Tom Morris, Pius Uzamere



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

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

Instance Attribute Details

#fragmentObject

Returns the value of attribute fragment.



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

def fragment
  @fragment
end

#shortObject

Returns the value of attribute short.



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

def short
  @short
end

#uriObject

Returns the value of attribute uri.



4
5
6
# File 'lib/rdfa_parser/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



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

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

#bind(graph) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/rdfa_parser/namespace.rb', line 61

def bind(graph)
  if graph.class == Graph
    graph.bind(self)
  else
    raise GraphException, "Can't bind namespace to graph of type #{graph.class}"
  end
end

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

Returns:

  • (Boolean)


69
70
71
# File 'lib/rdfa_parser/namespace.rb', line 69

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

#inspectObject



83
84
85
# File 'lib/rdfa_parser/namespace.rb', line 83

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

#xmlns_attrObject

Output xmlns attribute name



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

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

#xmlns_hashObject



79
80
81
# File 'lib/rdfa_parser/namespace.rb', line 79

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