lib/sxp/extensions.rb in sxp-1.2.0 vs lib/sxp/extensions.rb in sxp-1.2.1
- old
+ new
@@ -53,15 +53,31 @@
##
# Extensions for Ruby's `String` class.
class String
##
- # Returns the SXP representation of this object.
+ # Returns the SXP representation of this object. Uses SPARQL-like escaping.
#
# @return [String]
def to_sxp(**options)
- inspect
+ buffer = ""
+ each_char do |u|
+ buffer << case u.ord
+ when (0x00..0x07) then sprintf("\\u%04X", u.ord)
+ when (0x08) then '\b'
+ when (0x09) then '\t'
+ when (0x0A) then '\n'
+ when (0x0C) then '\f'
+ when (0x0D) then '\r'
+ when (0x0E..0x1F) then sprintf("\\u%04X", u.ord)
+ when (0x22) then '\"'
+ when (0x5C) then '\\\\'
+ when (0x7F) then sprintf("\\u%04X", u.ord)
+ else u.chr
+ end
+ end
+ '"' + buffer + '"'
end
end
##
# Extensions for Ruby's `Symbol` class.
@@ -227,11 +243,11 @@
end
end
class RDF::URI
##
- # Returns the SXP representation of this a URI. Uses Lexical representation, if set, otherwise, any PName match, otherwise, the relativized version of the URI if a base_uri is given, otherwise just the URI.
+ # Returns the SXP representation of this URI. Uses Lexical representation, if set, otherwise, any PName match, otherwise, the relativized version of the URI if a base_uri is given, otherwise just the URI.
#
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil, **options)
@@ -266,10 +282,10 @@
case datatype
when RDF::XSD.boolean, RDF::XSD.integer, RDF::XSD.double, RDF::XSD.decimal, RDF::XSD.time
# Retain stated lexical form if possible
valid? ? to_s : object.to_sxp(**options)
else
- text = value.dump
+ text = value.to_sxp
text << "@#{language}" if self.has_language?
text << "^^#{datatype.to_sxp(**options)}" if self.has_datatype?
text
end
end