Class: RdfaParser::Literal

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

Class Method Summary collapse

Instance Method Summary collapse

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, 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

Returns the value of attribute contents.



319
320
321
# File 'lib/rdfa_parser/literal.rb', line 319

def contents
  @contents
end

#encodingObject

Returns the value of attribute encoding.



319
320
321
# File 'lib/rdfa_parser/literal.rb', line 319

def encoding
  @encoding
end

#langObject

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, options = {})
  encoding = Encoding.coerce(encoding)
  new(contents, encoding, options)
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)
  options = {}
  options[:language] = language if language
  new(contents, Encoding.the_null_encoding, options)
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_n3Object 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_sObject

Output value



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

def to_s
  self.contents.to_s
end

#to_trixObject



383
384
385
# File 'lib/rdfa_parser/literal.rb', line 383

def to_trix
  encoding.format_as_trix(@contents, @lang)
end

#xml_argsObject



387
388
389
# File 'lib/rdfa_parser/literal.rb', line 387

def xml_args
  encoding.xml_args( @contents, @lang)
end

#xmlliteral?Boolean

Returns:

  • (Boolean)


391
392
393
# File 'lib/rdfa_parser/literal.rb', line 391

def xmlliteral?
  encoding.xmlliteral?
end