Class: 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
-
#contents ⇒ Object
Contents of Literal.
-
#encoding ⇒ Literal::Encoding
Encoding defined for literal.
-
#lang ⇒ String
Language associated with literal.
Class Method Summary collapse
-
.build_from(object) ⇒ Literal
Create a literal appropriate for type of object by datatype introspection.
-
.infer_encoding_for(object) ⇒ Encoding
Infer the proper XML datatype for the given object.
-
.n3_encoded(contents, language, encoding = nil) ⇒ Literal
Create literal from a string that is already N3 encoded.
-
.parse(str) ⇒ Object
Parse a Literal in NTriples format.
-
.typed(contents, encoding, options = {}) ⇒ Literal
Create a typed literal.
-
.untyped(contents, language = nil) ⇒ Literal
Create an un-typed literal with a language.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
Compare literal with another literal or a string.
- #hash ⇒ Object
-
#initialize(contents, encoding, options = {}) ⇒ Literal
constructor
Create a new Literal.
- #inspect ⇒ Object
-
#literal? ⇒ Boolean
Returns ‘true`.
-
#to_n3 ⇒ Object
(also: #to_ntriples)
Output literal in N3 format.
-
#to_native ⇒ Object
Create native representation for value.
-
#to_s ⇒ Object
Output literal contents as a string.
-
#to_trix ⇒ Object
Output literal in TriX format.
- #typed? ⇒ Boolean
- #untyped? ⇒ Boolean
- #valid? ⇒ Boolean
-
#xml_args ⇒ Object
Return content and hash appropriate for encoding in XML.
-
#xmlliteral? ⇒ Boolean
Is this an XMLLiteral?.
Methods inherited from Resource
#bnode?, #graph?, #resource?, #uri?
Constructor Details
#initialize(contents, encoding, options = {}) ⇒ Literal
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
#contents ⇒ Object
Contents of Literal
320 321 322 |
# File 'lib/rdf_context/literal.rb', line 320 def contents @contents end |
#encoding ⇒ Literal::Encoding
Encoding defined for literal
324 325 326 |
# File 'lib/rdf_context/literal.rb', line 324 def encoding @encoding end |
#lang ⇒ String
Language associated with literal
328 329 330 |
# File 'lib/rdf_context/literal.rb', line 328 def lang @lang end |
Class Method Details
.build_from(object) ⇒ Literal
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 |
.infer_encoding_for(object) ⇒ Encoding
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 |
.n3_encoded(contents, language, encoding = nil) ⇒ Literal
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 |
.parse(str) ⇒ Object
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 |
.typed(contents, encoding, options = {}) ⇒ Literal
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 |
.untyped(contents, language = nil) ⇒ Literal
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
#<=>(other) ⇒ Object
442 443 444 |
# File 'lib/rdf_context/literal.rb', line 442 def <=>(other) self.to_s <=> other.to_s end |
#==(other) ⇒ Object
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 |
#hash ⇒ Object
446 447 448 |
# File 'lib/rdf_context/literal.rb', line 446 def hash [@contents, @encoding, @lang].hash end |
#inspect ⇒ Object
456 457 458 |
# File 'lib/rdf_context/literal.rb', line 456 def inspect "#{self.class}[#{self.to_n3}]" end |
#literal? ⇒ Boolean
Returns ‘true`
496 497 498 |
# File 'lib/rdf_context/literal.rb', line 496 def literal? true end |
#to_n3 ⇒ Object 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 |
#to_native ⇒ Object
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 |
#to_s ⇒ Object
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 |
#to_trix ⇒ Object
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 |
#typed? ⇒ Boolean
501 |
# File 'lib/rdf_context/literal.rb', line 501 def typed?; encoding != Encoding.the_null_encoding; end |
#untyped? ⇒ Boolean
500 |
# File 'lib/rdf_context/literal.rb', line 500 def untyped?; encoding == Encoding.the_null_encoding; end |
#valid? ⇒ Boolean
460 461 462 |
# File 'lib/rdf_context/literal.rb', line 460 def valid? encoding.valid?(@contents) end |
#xml_args ⇒ Object
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 |
#xmlliteral? ⇒ Boolean
Is this an XMLLiteral?
504 |
# File 'lib/rdf_context/literal.rb', line 504 def xmlliteral?; encoding == Encoding.xmlliteral; end |