Class: RdfContext::Literal

Inherits:
Resource show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • contents (Object)
  • encoding (Encoding)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

Raises:



338
339
340
341
342
343
344
345
346
347
348
# File 'lib/rdf_context/literal.rb', line 338

def initialize(contents, encoding, options = {})
  unless encoding.is_a?(Encoding)
    raise TypeError, "#{encoding.inspect} should be an instance of Encoding"
  end
  @encoding = encoding
  lang = options[:language]
  @lang = Language.new(lang) if lang
  options = {:namespaces => {}}.merge(options)

  @contents = @encoding.encode_contents(contents, options)
end

Instance Attribute Details

#contentsObject

Contents of Literal



320
321
322
# File 'lib/rdf_context/literal.rb', line 320

def contents
  @contents
end

#encodingLiteral::Encoding

Encoding defined for literal

Returns:



324
325
326
# File 'lib/rdf_context/literal.rb', line 324

def encoding
  @encoding
end

#langString

Language associated with literal

Returns:



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

Parameters:

  • contents (Object)

Returns:

Raises:



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

Parameters:

  • contents (Object)

Returns:



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.

Parameters:

  • contents (Object)
  • language (String)
  • encoding (Encoding) (defaults to: nil)

    (nil)

Returns:

Raises:



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)
  options = {}
  options[:language] = language if language
  #puts "encoded: #{contents.dump}"
  contents = contents.rdf_unescape
  #puts "unencoded: #{contents.dump}"
  new(contents, encoding, options)
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

Parameters:

  • contents (Object)
  • encoding (Encoding)

    (nil)

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

Returns:

Raises:



395
396
397
398
# File 'lib/rdf_context/literal.rb', line 395

def self.typed(contents, encoding, options = {})
  encoding = Encoding.coerce(encoding)
  new(contents, encoding, options)
end

.untyped(contents, language = nil) ⇒ Literal

Create an un-typed literal with a language

Parameters:

  • contents (Object)
  • language (String) (defaults to: nil)

    (nil)

Returns:

Raises:



383
384
385
386
387
# File 'lib/rdf_context/literal.rb', line 383

def self.untyped(contents, language = nil)
  options = {}
  options[:language] = language if language
  new(contents, Encoding.the_null_encoding, options)
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

#hashObject



446
447
448
# File 'lib/rdf_context/literal.rb', line 446

def hash
  [@contents, @encoding, @lang].hash
end

#inspectObject



456
457
458
# File 'lib/rdf_context/literal.rb', line 456

def inspect
  "#{self.class}[#{self.to_n3}]"
end

#literal?Boolean

Returns ‘true`

Returns:

  • (Boolean)


496
497
498
# File 'lib/rdf_context/literal.rb', line 496

def literal?
  true
end

#to_n3Object 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_nativeObject

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_sObject

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_trixObject

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

Returns:

  • (Boolean)


501
# File 'lib/rdf_context/literal.rb', line 501

def typed?; encoding != Encoding.the_null_encoding; end

#untyped?Boolean

Returns:

  • (Boolean)


500
# File 'lib/rdf_context/literal.rb', line 500

def untyped?; encoding == Encoding.the_null_encoding; end

#valid?Boolean

Returns:

  • (Boolean)


460
461
462
# File 'lib/rdf_context/literal.rb', line 460

def valid?
  encoding.valid?(@contents)
end

#xml_argsObject

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?

Returns:

  • (Boolean)


504
# File 'lib/rdf_context/literal.rb', line 504

def xmlliteral?; encoding == Encoding.xmlliteral; end