Class: RdfContext::Literal
- Inherits:
-
Resource
- Object
- Resource
- RdfContext::Literal
- Defined in:
- lib/rdf_context/literal.rb
Overview
An RDF Literal, with value, encoding and language elements.
Defined Under Namespace
Classes: Encoding, Null, XMLLiteral
Instance Attribute Summary (collapse)
-
- (Object) contents
Contents of Literal.
-
- (Literal::Encoding) encoding
Encoding defined for literal.
-
- (String) lang
Language associated with literal.
Class Method Summary (collapse)
-
+ (Literal) build_from(object)
Create a literal appropriate for type of object by datatype introspection.
-
+ (Encoding) infer_encoding_for(object)
Infer the proper XML datatype for the given object.
-
+ (Literal) n3_encoded(contents, language, encoding = nil)
Create literal from a string that is already N3 encoded.
- + (Object) new protected
-
+ (Object) parse(str)
Parse a Literal in NTriples format.
-
+ (Literal) typed(contents, encoding, options = {})
Create a typed literal.
-
+ (Literal) untyped(contents, language = nil)
Create an un-typed literal with a language.
Instance Method Summary (collapse)
- - (Object) <=>(other)
-
- (Object) ==(other)
Compare literal with another literal or a string.
- - (Object) hash
-
- (Literal) initialize(contents, encoding, options = {})
constructor
Create a new Literal.
- - (Object) inspect
-
- (Boolean) literal?
Returns `true`.
-
- (Object) to_n3
(also: #to_ntriples)
Output literal in N3 format.
-
- (Object) to_native
Create native representation for value.
-
- (Object) to_s
Output literal contents as a string.
-
- (Object) to_trix
Output literal in TriX format.
- - (Boolean) typed?
- - (Boolean) untyped?
- - (Boolean) valid?
-
- (Object) xml_args
Return content and hash appropriate for encoding in XML.
-
- (Boolean) xmlliteral?
Is this an XMLLiteral?.
Methods inherited from Resource
#bnode?, #graph?, #resource?, #uri?
Constructor Details
- (Literal) initialize(contents, encoding, options = {})
Create a new Literal. Optinally pass a namespaces hash for use in applying to rdf::XMLLiteral values.
338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/rdf_context/literal.rb', line 338 def initialize(contents, encoding, = {}) unless encoding.is_a?(Encoding) raise TypeError, "#{encoding.inspect} should be an instance of Encoding" end @encoding = encoding lang = [:language] @lang = Language.new(lang) if lang = {:namespaces => {}}.merge() @contents = @encoding.encode_contents(contents, ) end |
Instance Attribute Details
- (Object) contents
Contents of Literal
320 321 322 |
# File 'lib/rdf_context/literal.rb', line 320 def contents @contents end |
- (Literal::Encoding) encoding
Encoding defined for literal
324 325 326 |
# File 'lib/rdf_context/literal.rb', line 324 def encoding @encoding end |
- (String) lang
Language associated with literal
328 329 330 |
# File 'lib/rdf_context/literal.rb', line 328 def lang @lang end |
Class Method Details
+ (Literal) build_from(object)
Create a literal appropriate for type of object by datatype introspection
404 405 406 |
# File 'lib/rdf_context/literal.rb', line 404 def self.build_from(object) new(object.to_s, infer_encoding_for(object)) end |
+ (Encoding) infer_encoding_for(object)
Infer the proper XML datatype for the given object
411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/rdf_context/literal.rb', line 411 def self.infer_encoding_for(object) case object when TrueClass then Encoding.boolean when FalseClass then Encoding.boolean when Integer then Encoding.integer when Float then Encoding.float when Time then Encoding.time when DateTime then Encoding.datetime when Date then Encoding.date when Duration then Encoding.duration else Encoding.string end end |
+ (Literal) n3_encoded(contents, language, encoding = nil)
Create literal from a string that is already N3 encoded.
356 357 358 359 360 361 362 363 364 |
# File 'lib/rdf_context/literal.rb', line 356 def self.n3_encoded(contents, language, encoding = nil) encoding = encoding.nil? ? Encoding.the_null_encoding : Encoding.coerce(encoding) = {} [:language] = language if language #puts "encoded: #{contents.dump}" contents = contents.rdf_unescape #puts "unencoded: #{contents.dump}" new(contents, encoding, ) end |
+ (Object) new (protected)
+ (Object) parse(str)
Parse a Literal in NTriples format
367 368 369 370 371 372 373 374 375 376 |
# File 'lib/rdf_context/literal.rb', line 367 def self.parse(str) case str when LITERAL_WITH_LANGUAGE Literal.n3_encoded($1, $2) when LITERAL_WITH_DATATYPE Literal.n3_encoded($1, nil, $2) when LITERAL_PLAIN Literal.n3_encoded($1, nil) end end |
+ (Literal) typed(contents, encoding, options = {})
Create a typed literal
395 396 397 398 |
# File 'lib/rdf_context/literal.rb', line 395 def self.typed(contents, encoding, = {}) encoding = Encoding.coerce(encoding) new(contents, encoding, ) end |
+ (Literal) untyped(contents, language = nil)
Create an un-typed literal with a language
383 384 385 386 387 |
# File 'lib/rdf_context/literal.rb', line 383 def self.untyped(contents, language = nil) = {} [:language] = language if language new(contents, Encoding.the_null_encoding, ) end |
Instance Method Details
- (Object) <=>(other)
442 443 444 |
# File 'lib/rdf_context/literal.rb', line 442 def <=>(other) self.to_s <=> other.to_s end |
- (Object) ==(other)
Compare literal with another literal or a string. If a string is passed, only contents must match. Otherwise, compare encoding types, contents and languages.
432 433 434 435 436 437 438 439 440 |
# File 'lib/rdf_context/literal.rb', line 432 def ==(other) case other when String then other == self.contents when self.class other.encoding == @encoding && @encoding.compare_contents(self.contents, other.contents, other.lang == @lang) else false end end |
- (Object) hash
446 447 448 |
# File 'lib/rdf_context/literal.rb', line 446 def hash [@contents, @encoding, @lang].hash end |
- (Object) inspect
456 457 458 |
# File 'lib/rdf_context/literal.rb', line 456 def inspect "#{self.class}[#{self.to_n3}]" end |
- (Boolean) literal?
Returns `true`
496 497 498 |
# File 'lib/rdf_context/literal.rb', line 496 def literal? true end |
- (Object) to_n3 Also known as: to_ntriples
Output literal in N3 format
451 452 453 |
# File 'lib/rdf_context/literal.rb', line 451 def to_n3 encoding.format_as_n3(self.contents, @lang) end |
- (Object) to_native
Create native representation for value
470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/rdf_context/literal.rb', line 470 def to_native case encoding when Encoding.boolean then @contents.to_s == "true" when Encoding.double then @contents.to_s.to_f when Encoding.integer then @contents.to_s.to_i when Encoding.float then @contents.to_s.to_f when Encoding.time then Time.parse(@contents.to_s) when Encoding.datetime then DateTime.parse(@contents.to_s) when Encoding.date then Date.parse(@contents.to_s) when Encoding.duration then Duration.parse(@contents.to_s) else @contents.to_s end end |
- (Object) to_s
Output literal contents as a string
507 508 509 |
# File 'lib/rdf_context/literal.rb', line 507 def to_s self.contents.to_s end |
- (Object) to_trix
Output literal in TriX format
465 466 467 |
# File 'lib/rdf_context/literal.rb', line 465 def to_trix encoding.format_as_trix(@contents, @lang) end |
- (Boolean) typed?
501 |
# File 'lib/rdf_context/literal.rb', line 501 def typed?; encoding != Encoding.the_null_encoding; end |
- (Boolean) untyped?
500 |
# File 'lib/rdf_context/literal.rb', line 500 def untyped?; encoding == Encoding.the_null_encoding; end |
- (Boolean) valid?
460 461 462 |
# File 'lib/rdf_context/literal.rb', line 460 def valid? encoding.valid?(@contents) end |
- (Object) xml_args
Return content and hash appropriate for encoding in XML
Example
Encoding.the_null_encoding.xml_args("foo", "en-US") => ["foo", {"xml:lang" => "en-US"}]
488 489 490 |
# File 'lib/rdf_context/literal.rb', line 488 def xml_args encoding.xml_args(@contents, @lang) end |
- (Boolean) xmlliteral?
Is this an XMLLiteral?
504 |
# File 'lib/rdf_context/literal.rb', line 504 def xmlliteral?; encoding == Encoding.xmlliteral; end |