Class: RdfContext::Namespace
- Inherits:
-
Object
- Object
- RdfContext::Namespace
- Defined in:
- lib/rdf_context/namespace.rb
Overview
From RdfContext
Instance Attribute Summary collapse
-
#fragment ⇒ Object
Returns the value of attribute fragment.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Instance Method Summary collapse
-
#+(suffix) ⇒ URIRef
Construct a URIRef from a namespace as in method_missing, but without method collision issues.
-
#bind(graph) ⇒ Namespace
Bind this namespace to a Graph.
-
#eql?(other) ⇒ Boolean
(also: #==)
Compare namespaces.
-
#initialize(uri, prefix) ⇒ Namespace
constructor
Creates a new namespace given a URI and the prefix.
- #inspect ⇒ Object
-
#method_missing(methodname, *args) ⇒ URIRef
Allows the construction of arbitrary URIs on the namespace.
- #to_s ⇒ String
-
#uri ⇒ URIRef
Make sure to attach fragment.
-
#xmlns_attr ⇒ String
Output xmlns attribute name.
-
#xmlns_hash ⇒ Hash{String => String}
Output namespace definition as a hash.
Constructor Details
#initialize(uri, prefix) ⇒ 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
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
#method_missing(methodname, *args) ⇒ URIRef
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.
43 44 45 |
# File 'lib/rdf_context/namespace.rb', line 43 def method_missing(methodname, *args) self + methodname end |
Instance Attribute Details
#fragment ⇒ Object
Returns the value of attribute fragment.
4 5 6 |
# File 'lib/rdf_context/namespace.rb', line 4 def fragment @fragment end |
#prefix ⇒ Object
Returns the value of attribute prefix.
4 5 6 |
# File 'lib/rdf_context/namespace.rb', line 4 def prefix @prefix end |
Instance Method Details
#+(suffix) ⇒ URIRef
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 |
#bind(graph) ⇒ Namespace
Bind this namespace to a Graph
68 69 70 |
# File 'lib/rdf_context/namespace.rb', line 68 def bind(graph) graph.bind(self) end |
#eql?(other) ⇒ Boolean Also known as: ==
Compare namespaces
75 76 77 |
# File 'lib/rdf_context/namespace.rb', line 75 def eql?(other) self.uri == other.uri end |
#inspect ⇒ Object
97 98 99 |
# File 'lib/rdf_context/namespace.rb', line 97 def inspect "Namespace[abbr='#{prefix}',uri='#{@uri}']" end |
#to_s ⇒ String
93 94 95 |
# File 'lib/rdf_context/namespace.rb', line 93 def to_s "#{prefix}: #{@uri}" end |
#uri ⇒ URIRef
Make sure to attach fragment
61 62 63 |
# File 'lib/rdf_context/namespace.rb', line 61 def uri self + "" end |
#xmlns_attr ⇒ String
Output xmlns attribute name
82 83 84 |
# File 'lib/rdf_context/namespace.rb', line 82 def xmlns_attr prefix.empty? ? "xmlns" : "xmlns:#{prefix}" end |