Class: ActiveRDF::Namespace
- Inherits:
-
Object
- Object
- ActiveRDF::Namespace
- Defined in:
- lib/active_rdf/objectmanager/namespace.rb
Constant Summary collapse
- @@namespaces =
Hash.new
- @@inverted_namespaces =
Hash.new
Class Method Summary collapse
-
.abbreviate(obj) ⇒ Object
abbreviates resource if namespace is registered, otherwise returns nil.
-
.abbreviations ⇒ Object
returns currently registered namespace abbreviations (e.g. :foaf, :rdf).
-
.expand(prefix, localname) ⇒ Object
returns URI (string) formed by concatenation of prefix and localname.
-
.find(name) ⇒ Object
like include?, but returns the key for the namespace.
- .include?(name) ⇒ Boolean
-
.localname(obj) ⇒ Object
returns local-part of URI.
-
.lookup(prefix, localname) ⇒ Object
returns a resource whose URI is formed by concatenation of prefix and localname.
-
.prefix(obj) ⇒ Object
returns prefix (if known) for the non-local part of the URI, or nil if prefix not registered.
-
.register(prefix, fullURI) ⇒ Object
registers a namespace prefix and its associated expansion (full URI) e.g.
Class Method Details
.abbreviate(obj) ⇒ Object
abbreviates resource if namespace is registered, otherwise returns nil
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/active_rdf/objectmanager/namespace.rb', line 90 def Namespace.abbreviate(obj) uri = obj.to_s # uri.to_s gives us the uri of the resource (if resource given) # then we find the last occurrence of # or / (heuristical namespace # delimitor) delimiter = uri.rindex(/#|\//) if delimiter.nil? or delimiter == uri.size-1 abbr = @@inverted_namespaces[uri] abbr.to_s if (abbr) else abbr = @@inverted_namespaces[uri[0..delimiter]] abbr.to_s.upcase + "::" + uri[delimiter+1..-1] if (abbr) end end |
.abbreviations ⇒ Object
returns currently registered namespace abbreviations (e.g. :foaf, :rdf)
35 36 37 |
# File 'lib/active_rdf/objectmanager/namespace.rb', line 35 def Namespace.abbreviations @@namespaces.keys end |
.expand(prefix, localname) ⇒ Object
returns URI (string) formed by concatenation of prefix and localname
55 56 57 58 59 |
# File 'lib/active_rdf/objectmanager/namespace.rb', line 55 def Namespace.(prefix, localname) prefix = prefix.downcase if prefix.is_a?String prefix = prefix.to_sym @@namespaces[prefix].to_s + localname.to_s if @@namespaces[prefix] end |
.find(name) ⇒ Object
like include?, but returns the key for the namespace
44 45 46 47 |
# File 'lib/active_rdf/objectmanager/namespace.rb', line 44 def Namespace.find(name) name = name.to_s.downcase.to_sym @@namespaces.keys.find{|k| k == name} end |
.include?(name) ⇒ Boolean
39 40 41 |
# File 'lib/active_rdf/objectmanager/namespace.rb', line 39 def Namespace.include?(name) @@namespaces.keys.include?(name.to_s.downcase.to_sym) end |
.localname(obj) ⇒ Object
returns local-part of URI
79 80 81 82 83 84 85 86 87 |
# File 'lib/active_rdf/objectmanager/namespace.rb', line 79 def Namespace.localname(obj) uri = obj.respond_to?(:uri) ? obj.uri : obj.to_s delimiter = uri.rindex(/#|\//) if delimiter.nil? or delimiter == uri.size-1 uri else uri[delimiter+1..-1] end end |
.lookup(prefix, localname) ⇒ Object
returns a resource whose URI is formed by concatenation of prefix and localname
50 51 52 |
# File 'lib/active_rdf/objectmanager/namespace.rb', line 50 def Namespace.lookup(prefix, localname) RDFS::Resource.new((prefix, localname)) end |
.prefix(obj) ⇒ Object
returns prefix (if known) for the non-local part of the URI, or nil if prefix not registered
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_rdf/objectmanager/namespace.rb', line 63 def Namespace.prefix(obj) uri = obj.respond_to?(:uri) ? obj.uri : obj.to_s # uri.to_s gives us the uri of the resource (if resource given) # then we find the last occurrence of # or / (heuristical namespace # delimitor) delimiter = uri.rindex(/#|\//) # if delimiter not found, URI cannot be split into (non)local-part return uri if delimiter.nil? # extract non-local part (including delimiter) nonlocal = uri[0..delimiter] @@inverted_namespaces[nonlocal] end |
.register(prefix, fullURI) ⇒ Object
registers a namespace prefix and its associated expansion (full URI) e.g. :rdf and ‘www.w3.org/1999/02/22-rdf-syntax-ns#’
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/active_rdf/objectmanager/namespace.rb', line 11 def Namespace.register(prefix, fullURI) raise ActiveRdfError, 'prefix nor uri can be empty' if (prefix.to_s.empty? or fullURI.to_s.empty?) raise ActiveRdfError, "namespace uri should end with # or /" unless /\/|#/ =~ fullURI.to_s[-1..-1] klass_name = prefix.to_s.upcase prefix = prefix.to_s.downcase.to_sym ActiveRdfLogger::log_info(self) { "Namespace: registering #{fullURI} to #{klass_name}" } @@namespaces[prefix] = fullURI.to_s @@inverted_namespaces[fullURI.to_s] = prefix # enable namespace lookups through FOAF::name # if FOAF defined, extend it if Object.const_defined?(klass_name) ns = Object.const_get(klass_name) else # otherwise create a new module for it ns = Module.new Object.const_set(klass_name, ns) end ns.extend NamespaceProxy ns.prefix = prefix ns end |