Class: RdfaParser::Namespace
- Inherits:
-
Object
- Object
- RdfaParser::Namespace
- Defined in:
- lib/rdfa_parser/namespace.rb
Overview
From Reddy
Instance Attribute Summary collapse
-
#fragment ⇒ Object
Returns the value of attribute fragment.
-
#short ⇒ Object
Returns the value of attribute short.
-
#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
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(uri, short, fragment = nil) ⇒ Namespace
constructor
Creates a new namespace given a URI and the short name.
- #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
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
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
52 53 54 |
# File 'lib/rdfa_parser/namespace.rb', line 52 def method_missing(methodname, *args) self + methodname end |
Instance Attribute Details
#fragment ⇒ Object
Returns the value of attribute fragment.
4 5 6 |
# File 'lib/rdfa_parser/namespace.rb', line 4 def fragment @fragment end |
#short ⇒ Object
Returns the value of attribute short.
4 5 6 |
# File 'lib/rdfa_parser/namespace.rb', line 4 def short @short end |
#uri ⇒ Object
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: ==
69 70 71 |
# File 'lib/rdfa_parser/namespace.rb', line 69 def eql?(other) @short == other.short && @uri == other.uri && @fragment == other.fragment end |
#inspect ⇒ Object
83 84 85 |
# File 'lib/rdfa_parser/namespace.rb', line 83 def inspect "Namespace[abbr='#{short}',uri='#{uri}']" end |
#xmlns_attr ⇒ Object
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_hash ⇒ Object
79 80 81 |
# File 'lib/rdfa_parser/namespace.rb', line 79 def xmlns_hash {xmlns_attr => @uri.to_s} end |