Class: Reddy::Literal

Inherits:
Object
  • Object
show all
Defined in:
lib/reddy/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.



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/reddy/literal.rb', line 235

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.



231
232
233
# File 'lib/reddy/literal.rb', line 231

def contents
  @contents
end

#encodingObject

Returns the value of attribute encoding.



231
232
233
# File 'lib/reddy/literal.rb', line 231

def encoding
  @encoding
end

#langObject

Returns the value of attribute lang.



231
232
233
# File 'lib/reddy/literal.rb', line 231

def lang
  @lang
end

Class Method Details

.build_from(object) ⇒ Object

Create a literal appropriate for type of object by datatype introspection



274
275
276
# File 'lib/reddy/literal.rb', line 274

def self.build_from(object)
  new(object.to_s, infer_encoding_for(object))
end

.infer_encoding_for(object) ⇒ Object

Infer the proper XML datatype for the given object



279
280
281
282
283
284
285
286
287
288
# File 'lib/reddy/literal.rb', line 279

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

.n3_encoded(contents, language, encoding = nil) ⇒ Object

Create literal from a string that is already N3 encoded.



248
249
250
251
252
253
254
255
256
# File 'lib/reddy/literal.rb', line 248

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

.typed(contents, encoding, options = {}) ⇒ Object

Create a typed literal Options include:

namespaces

A hash of namespace entries (for XMLLiteral)



268
269
270
271
# File 'lib/reddy/literal.rb', line 268

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

.untyped(contents, language = nil) ⇒ Object

Create an un-typed literal with a language



259
260
261
262
263
# File 'lib/reddy/literal.rb', line 259

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

Compare literal with another literal or a string. If a string is passed, only contents must match. Otherwise, compare encoding types, contents and languages.



297
298
299
300
301
302
303
304
305
# File 'lib/reddy/literal.rb', line 297

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



307
308
309
# File 'lib/reddy/literal.rb', line 307

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

#to_n3Object Also known as: to_ntriples

Output literal in N3 format



312
313
314
# File 'lib/reddy/literal.rb', line 312

def to_n3
  encoding.format_as_n3(self.contents, @lang)
end

#to_sObject

Output literal contents as a string



336
337
338
# File 'lib/reddy/literal.rb', line 336

def to_s
  self.contents.to_s
end

#to_trixObject

Output literal in TriX format



318
319
320
# File 'lib/reddy/literal.rb', line 318

def to_trix
  encoding.format_as_trix(@contents, @lang)
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"}]


326
327
328
# File 'lib/reddy/literal.rb', line 326

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

#xmlliteral?Boolean

Is this an XMLLiteral?

Returns:

  • (Boolean)


331
332
333
# File 'lib/reddy/literal.rb', line 331

def xmlliteral?
  encoding.is_a?(XMLLiteral)
end