Class: Reddy::Namespace

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, short, fragment = false) ⇒ 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

Parameters:

  • uri (String)

    the URI of the namespace

  • short (String)

    the short name of the namespace

  • fragment (Boolean) (defaults to: false)

    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



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

def initialize(uri, short, fragment = false)
  @uri = uri
  @fragment = fragment
  if shortname_valid?(short)
    @short = short
  else
    raise
  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



48
49
50
51
52
53
54
# File 'lib/reddy/namespace.rb', line 48

def method_missing(methodname, *args)
  unless fragment
    URIRef.new(@uri + methodname.to_s)
  else
    URIRef.new(@uri + '#' + methodname.to_s)
  end
end

Instance Attribute Details

#fragmentObject

Returns the value of attribute fragment.



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

def fragment
  @fragment
end

#shortObject

Returns the value of attribute short.



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

def short
  @short
end

#uriObject

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#bind(graph) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/reddy/namespace.rb', line 56

def bind(graph)
  if graph.class == Graph
    graph.bind(self)
  else
    raise
  end
end