Class: RdfaParser::Literal
- Inherits:
-
Object
- Object
- RdfaParser::Literal
- Defined in:
- lib/rdfa_parser/literal.rb
Overview
An RDF Literal, with value, encoding and language elements.
Defined Under Namespace
Classes: Encoding, Language, Null, XMLLiteral
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#lang ⇒ Object
Returns the value of attribute lang.
Class Method Summary collapse
- .build_from(object) ⇒ Object
- .infer_encoding_for(object) ⇒ Object
-
.typed(contents, encoding, options = {}) ⇒ Object
- Options include: namespaces
- A hash of namespace entries (for XMLLiteral) language
-
Language encoding.
- .untyped(contents, language = nil) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(contents, encoding, options = {}) ⇒ Literal
constructor
Create a new Literal.
- #to_n3 ⇒ Object (also: #to_ntriples)
-
#to_s ⇒ Object
Output value.
- #to_trix ⇒ Object
- #xml_args ⇒ Object
- #xmlliteral? ⇒ Boolean
Constructor Details
#initialize(contents, encoding, options = {}) ⇒ Literal
Create a new Literal. Optinally pass a namespaces hash for use in applying to rdf::XMLLiteral values.
323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/rdfa_parser/literal.rb', line 323 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
Returns the value of attribute contents.
319 320 321 |
# File 'lib/rdfa_parser/literal.rb', line 319 def contents @contents end |
#encoding ⇒ Object
Returns the value of attribute encoding.
319 320 321 |
# File 'lib/rdfa_parser/literal.rb', line 319 def encoding @encoding end |
#lang ⇒ Object
Returns the value of attribute lang.
319 320 321 |
# File 'lib/rdfa_parser/literal.rb', line 319 def lang @lang end |
Class Method Details
.build_from(object) ⇒ Object
349 350 351 |
# File 'lib/rdfa_parser/literal.rb', line 349 def self.build_from(object) new(object.to_s, infer_encoding_for(object)) end |
.infer_encoding_for(object) ⇒ Object
353 354 355 356 357 358 359 360 361 362 |
# File 'lib/rdfa_parser/literal.rb', line 353 def self.infer_encoding_for(object) case object when Integer then Encoding.new("http://www.w3.org/2001/XMLSchema#int") when Float then Encoding.new("http://www.w3.org/2001/XMLSchema#float") when Time then Encoding.new("http://www.w3.org/2001/XMLSchema#time") when DateTime then Encoding.new("http://www.w3.org/2001/XMLSchema#dateTime") when Date then Encoding.new("http://www.w3.org/2001/XMLSchema#date") else Encoding.new("http://www.w3.org/2001/XMLSchema#string") end end |
.typed(contents, encoding, options = {}) ⇒ Object
Options include:
- namespaces
-
A hash of namespace entries (for XMLLiteral)
- language
-
Language encoding
344 345 346 347 |
# File 'lib/rdfa_parser/literal.rb', line 344 def self.typed(contents, encoding, = {}) encoding = Encoding.coerce(encoding) new(contents, encoding, ) end |
.untyped(contents, language = nil) ⇒ Object
335 336 337 338 339 |
# File 'lib/rdfa_parser/literal.rb', line 335 def self.untyped(contents, language = nil) = {} [:language] = language if language new(contents, Encoding.the_null_encoding, ) end |
Instance Method Details
#==(other) ⇒ Object
368 369 370 371 372 373 374 375 376 |
# File 'lib/rdfa_parser/literal.rb', line 368 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 |
#to_n3 ⇒ Object Also known as: to_ntriples
378 379 380 |
# File 'lib/rdfa_parser/literal.rb', line 378 def to_n3 encoding.format_as_n3(self.contents, @lang) end |
#to_s ⇒ Object
Output value
396 397 398 |
# File 'lib/rdfa_parser/literal.rb', line 396 def to_s self.contents.to_s end |
#to_trix ⇒ Object
383 384 385 |
# File 'lib/rdfa_parser/literal.rb', line 383 def to_trix encoding.format_as_trix(@contents, @lang) end |
#xml_args ⇒ Object
387 388 389 |
# File 'lib/rdfa_parser/literal.rb', line 387 def xml_args encoding.xml_args( @contents, @lang) end |
#xmlliteral? ⇒ Boolean
391 392 393 |
# File 'lib/rdfa_parser/literal.rb', line 391 def xmlliteral? encoding.xmlliteral? end |