Class: RdfContext::URIRef
- Defined in:
- lib/rdf_context/uriref.rb
Instance Attribute Summary collapse
-
#namespace(namespaces = []) ⇒ Namespace
readonly
Look at namespaces and find first that matches this URI, ordering by longest URI first.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
-
.intern(*args) ⇒ Object
Internalized version of URIRef.
-
.parse(str) ⇒ Object
Parse a URIRef.
Instance Method Summary collapse
-
#+(input) ⇒ URIRef
Create a URI, either by appending a fragment, or using the input URI.
- #<=>(other) ⇒ -1, 0 1
-
#base ⇒ String
base of URI for creating QNames.
- #eql?(other) ⇒ Boolean (also: #==)
-
#hash ⇒ String
Needed for uniq.
-
#initialize(*args) ⇒ URIRef
constructor
Create a URIRef from a URI or a fragment and a URI.
- #inspect ⇒ Object
-
#short_name ⇒ String, false
short_name of URI for creating QNames.
- #test_string(string) ⇒ Object protected
- #to_n3 ⇒ String (also: #to_ntriples)
-
#to_qname(uri_binding = []) ⇒ String
Output URI as QName using URI binding.
- #to_s ⇒ String
-
#uri? ⇒ Boolean
Returns ‘true`.
-
#xml_args ⇒ Array<Hash{String => String}>
Output URI as resource reference for RDF/XML.
Methods inherited from Resource
#bnode?, #graph?, #literal?, #resource?
Constructor Details
#initialize(*args) ⇒ URIRef
Create a URIRef from a URI or a fragment and a URI
Last argument may be an options hash to set:
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rdf_context/uriref.rb', line 18 def initialize (*args) = args.last.is_a?(Hash) ? args.pop : { :normalize => false } @normalize = [:normalize] @namespace = [:namespace] args.each {|s| test_string(s)} if args.size == 1 uri = Addressable::URI.parse(args[0].to_s.rdf_unescape.rdf_escape) else uri = Addressable::URI.join(*args.compact.map{|s| s.to_s.rdf_unescape.rdf_escape}.reverse) end raise ParserException, "<" + uri.to_s + "> is a relative URI" if uri.relative? # Unique URI through class hash to ensure that URIRefs can be easily compared @@uri_hash ||= {} @uri = @@uri_hash["#{uri}#{@normalize}"] ||= begin # Special case if URI has no path, and the authority ends with a '#' #uri = Addressable::URI.parse($1) if @normalize && uri.to_s.match(/^(.*)\#$/) #@normalize ? uri.normalize : uri uri end.freeze end |
Instance Attribute Details
#namespace(namespaces = []) ⇒ Namespace (readonly)
Look at namespaces and find first that matches this URI, ordering by longest URI first
142 143 144 |
# File 'lib/rdf_context/uriref.rb', line 142 def namespace @namespace end |
#uri ⇒ Object
Returns the value of attribute uri.
5 6 7 |
# File 'lib/rdf_context/uriref.rb', line 5 def uri @uri end |
Class Method Details
Instance Method Details
#+(input) ⇒ URIRef
Create a URI, either by appending a fragment, or using the input URI
65 66 67 68 |
# File 'lib/rdf_context/uriref.rb', line 65 def + (input) input_uri = Addressable::URI.parse(input.to_s) return URIRef.intern(input_uri, self.to_s) end |
#<=>(other) ⇒ -1, 0 1
108 109 110 |
# File 'lib/rdf_context/uriref.rb', line 108 def <=>(other) self.to_s <=> other.to_s end |
#base ⇒ String
base of URI for creating QNames.
"#{base]{#short_name}}" == uri
91 92 93 94 95 96 97 |
# File 'lib/rdf_context/uriref.rb', line 91 def base @base ||= begin uri_base = self.to_s sn = short_name ? short_name.to_s : "" uri_base[0, uri_base.length - sn.length] end end |
#eql?(other) ⇒ Boolean Also known as: ==
101 102 103 |
# File 'lib/rdf_context/uriref.rb', line 101 def eql?(other) self.to_s == other.to_s end |
#hash ⇒ String
Needed for uniq
114 |
# File 'lib/rdf_context/uriref.rb', line 114 def hash; to_s.hash; end |
#inspect ⇒ Object
147 148 149 |
# File 'lib/rdf_context/uriref.rb', line 147 def inspect "#{self.class}[#{self.to_n3}, ns=#{namespace.inspect}]" end |
#short_name ⇒ String, false
short_name of URI for creating QNames.
"#{base]{#short_name}}" == uri
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rdf_context/uriref.rb', line 73 def short_name @short_name ||= begin path = @uri.path.split("/") if @namespace self.to_s.sub(@namespace.uri.to_s, "") elsif @uri.fragment @uri.fragment elsif path && path.length > 1 && path.last.class == String && path.last.length > 0 && path.last.index("/") != 0 path.last else false end end end |
#test_string(string) ⇒ Object (protected)
158 159 160 161 162 163 164 |
# File 'lib/rdf_context/uriref.rb', line 158 def test_string (string) string.to_s.each_byte do |b| if b >= 0 and b <= 31 raise ParserException, "URI '#{string}' must not contain control characters" end end end |
#to_n3 ⇒ String Also known as: to_ntriples
122 123 124 |
# File 'lib/rdf_context/uriref.rb', line 122 def to_n3 "<" + self.to_s + ">" end |
#to_qname(uri_binding = []) ⇒ String
Output URI as QName using URI binding
129 130 131 132 133 134 135 136 137 |
# File 'lib/rdf_context/uriref.rb', line 129 def to_qname(uri_binding = []) namespaces = case uri_binding when Hash then uri_binding.values when Array then uri_binding else [] end ns = namespace(namespaces) "#{ns.prefix}:#{short_name}" if ns end |
#to_s ⇒ String
117 118 119 |
# File 'lib/rdf_context/uriref.rb', line 117 def to_s @to_s ||= @uri.to_s end |
#uri? ⇒ Boolean
Returns ‘true`
47 48 49 |
# File 'lib/rdf_context/uriref.rb', line 47 def uri? true end |