Class: Reddy::Namespace
- Inherits:
-
Object
- Object
- Reddy::Namespace
- Defined in:
- lib/reddy/namespace.rb
Overview
From Reddy
Instance Attribute Summary collapse
-
#fragment ⇒ Object
Returns the value of attribute fragment.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#+(suffix) ⇒ Object
Construct a URIRef from a namespace as in method_missing, but without method collision issues.
-
#bind(graph) ⇒ Object
Bind this namespace to a Graph.
-
#eql?(other) ⇒ Boolean
(also: #==)
Compare namespaces.
-
#initialize(uri, prefix, fragment = nil) ⇒ 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.
-
#xmlns_attr ⇒ Object
Output xmlns attribute name.
-
#xmlns_hash ⇒ Object
Output namespace definition as a hash.
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
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"
42 43 44 |
# File 'lib/reddy/namespace.rb', line 42 def method_missing(methodname, *args) self + methodname end |
Instance Attribute Details
#fragment ⇒ Object
Returns the value of attribute fragment.
4 5 6 |
# File 'lib/reddy/namespace.rb', line 4 def fragment @fragment end |
#prefix ⇒ Object
Returns the value of attribute prefix.
4 5 6 |
# File 'lib/reddy/namespace.rb', line 4 def prefix @prefix end |
#uri ⇒ Object
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
57 58 59 |
# File 'lib/reddy/namespace.rb', line 57 def eql?(other) @prefix == other.prefix && @uri == other.uri && @fragment == other.fragment end |
#inspect ⇒ Object
72 73 74 |
# File 'lib/reddy/namespace.rb', line 72 def inspect "Namespace[abbr='#{prefix}',uri='#{uri}']" end |
#xmlns_attr ⇒ Object
Output xmlns attribute name
63 64 65 |
# File 'lib/reddy/namespace.rb', line 63 def xmlns_attr prefix.nil? ? "xmlns" : "xmlns:#{prefix}" end |
#xmlns_hash ⇒ Object
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 |