Class: RDF::Literal

Inherits:
Object
  • Object
show all
Includes:
Term
Defined in:
lib/rdf/model/literal.rb,
lib/rdf/model/literal/xml.rb,
lib/rdf/model/literal/time.rb,
lib/rdf/model/literal/date.rb,
lib/rdf/model/literal/token.rb,
lib/rdf/model/literal/double.rb,
lib/rdf/model/literal/decimal.rb,
lib/rdf/model/literal/boolean.rb,
lib/rdf/model/literal/numeric.rb,
lib/rdf/model/literal/integer.rb,
lib/rdf/model/literal/datetime.rb

Overview

An RDF literal.

Examples:

Creating a plain literal

value = RDF::Literal.new("Hello, world!")
value.plain?                                   #=> true

Creating a language-tagged literal (1)

value = RDF::Literal.new("Hello!", :language => :en)
value.has_language?                            #=> true
value.language                                 #=> :en

Creating a language-tagged literal (2)

RDF::Literal.new("Wazup?", :language => :"en-US")
RDF::Literal.new("Hej!",   :language => :sv)
RDF::Literal.new("¡Hola!", :language => :es)

Creating an explicitly datatyped literal

value = RDF::Literal.new("2009-12-31", :datatype => RDF::XSD.date)
value.has_datatype?                            #=> true
value.datatype                                 #=> RDF::XSD.date

Creating an implicitly datatyped literal

value = RDF::Literal.new(Date.today)
value.has_datatype?                            #=> true
value.datatype                                 #=> RDF::XSD.date

Creating implicitly datatyped literals

RDF::Literal.new(false).datatype               #=> XSD.boolean
RDF::Literal.new(true).datatype                #=> XSD.boolean
RDF::Literal.new(123).datatype                 #=> XSD.integer
RDF::Literal.new(9223372036854775807).datatype #=> XSD.integer
RDF::Literal.new(3.1415).datatype              #=> XSD.double
RDF::Literal.new(Time.now).datatype            #=> XSD.dateTime
RDF::Literal.new(Date.new(2010)).datatype      #=> XSD.date
RDF::Literal.new(DateTime.new(2010)).datatype  #=> XSD.dateTime

See Also:

Direct Known Subclasses

Boolean, Date, DateTime, Decimal, Double, Time, Token, XML

Defined Under Namespace

Modules: Numeric Classes: Boolean, Date, DateTime, Decimal, Double, Integer, Time, Token, XML

Constant Summary

TRUE =
RDF::Literal.new(true).freeze
FALSE =
RDF::Literal.new(false).freeze
ZERO =
RDF::Literal.new(0).freeze

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Term

#<=>, #constant?, #variable?

Methods included from Value

#graph?, #inspect!, #iri?, #node?, #resource?, #statement?, #to_ntriples, #to_rdf, #uri?, #variable?

Constructor Details

- (Literal) initialize(value, options = {})

A new instance of Literal

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Symbol) :language — default: nil
  • (URI) :datatype — default: nil


123
124
125
126
127
128
# File 'lib/rdf/model/literal.rb', line 123

def initialize(value, options = {})
  @object   = value
  @string   = options[:lexical] if options[:lexical]
  @language = options[:language].to_s.to_sym if options[:language]
  @datatype = RDF::URI(options[:datatype]) if options[:datatype]
end

Instance Attribute Details

- (URI) datatype

The XML Schema datatype URI (optional).

Returns:

  • (URI)

    The XML Schema datatype URI (optional).



117
118
119
# File 'lib/rdf/model/literal.rb', line 117

def datatype
  @datatype
end

- (Symbol) language

The language tag (optional).

Returns:

  • (Symbol)

    The language tag (optional).



114
115
116
# File 'lib/rdf/model/literal.rb', line 114

def language
  @language
end

Instance Method Details

- (Boolean) ==(other) Also known as: ===

Returns true if this literal is equivalent to other.

Examples:

RDF::Literal(1) == RDF::Literal(1.0)     #=> true

Parameters:

  • (Object) other

Returns:



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rdf/model/literal.rb', line 214

def ==(other)
  case other
    when Literal
      self.value.eql?(other.value) &&
      self.language.eql?(other.language) &&
      self.datatype.eql?(other.datatype)
    when String
      self.plain? && self.value.eql?(other)
    else false
  end
end

- (Boolean) anonymous?

Returns false.

Returns:



179
180
181
# File 'lib/rdf/model/literal.rb', line 179

def anonymous?
  false
end

- (RDF::Literal) canonicalize

Returns a copy of this literal converted into its canonical lexical representation.

Subclasses should override #canonicalize! as needed and appropriate, not this method.

Returns:

Since:

  • 0.2.1



302
303
304
# File 'lib/rdf/model/literal.rb', line 302

def canonicalize
  self.dup.canonicalize!
end

- (RDF::Literal) canonicalize!

Converts this literal into its canonical lexical representation.

Subclasses should override this as needed and appropriate.

Returns:

Since:

  • 0.3.0



313
314
315
316
# File 'lib/rdf/model/literal.rb', line 313

def canonicalize!
  @language = @language.to_s.downcase.to_sym if @language
  self
end

- (Boolean) eql?(other)

Returns true if this literal is equal to other.

Examples:

RDF::Literal(1).eql?(RDF::Literal(1.0))  #=> false

Parameters:

  • (Object) other

Returns:



199
200
201
202
203
204
# File 'lib/rdf/model/literal.rb', line 199

def eql?(other)
  self.equal?(other) ||
    (self.class.eql?(other.class) &&
     self.datatype.eql?(other.datatype) &&
     self == other)
end

- (Boolean) has_datatype? Also known as: datatype?, datatyped?, typed?

Returns true if this is a datatyped literal.

Returns:

See Also:



252
253
254
# File 'lib/rdf/model/literal.rb', line 252

def has_datatype?
  !datatype.nil?
end

- (Boolean) has_language? Also known as: language?

Returns true if this is a language-tagged literal.

Returns:

See Also:



242
243
244
# File 'lib/rdf/model/literal.rb', line 242

def has_language?
  !language.nil?
end

- (Fixnum) hash

Returns a hash code for this literal.

Returns:

  • (Fixnum)


187
188
189
# File 'lib/rdf/model/literal.rb', line 187

def hash
  to_s.hash
end

- (String) inspect

Returns a developer-friendly representation of self.

Returns:

  • (String)


330
331
332
# File 'lib/rdf/model/literal.rb', line 330

def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, RDF::NTriples.serialize(self))
end

- (Boolean) invalid?

Returns true if the value does not adhere to the defined grammar of the datatype.

Returns:

Since:

  • 0.2.1



276
277
278
# File 'lib/rdf/model/literal.rb', line 276

def invalid?
  !valid?
end

- (Boolean) literal?

Returns true.

Returns:



171
172
173
# File 'lib/rdf/model/literal.rb', line 171

def literal?
  true
end

- (Object) object

Returns:

  • (Object)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rdf/model/literal.rb', line 140

def object
  @object || case datatype
    when XSD.string, nil
      value
    when XSD.boolean
      %w(true 1).include?(value)
    when XSD.integer, XSD.long, XSD.int, XSD.short, XSD.byte
      value.to_i
    when XSD.double, XSD.float
      value.to_f
    when XSD.decimal
      ::BigDecimal.new(value)
    when XSD.date
      ::Date.parse(value)
    when XSD.dateTime
      ::DateTime.parse(value)
    when XSD.time
      ::Time.parse(value)
    when XSD.nonPositiveInteger, XSD.negativeInteger
      value.to_i
    when XSD.nonNegativeInteger, XSD.positiveInteger
      value.to_i
    when XSD.unsignedLong, XSD.unsignedInt, XSD.unsignedShort, XSD.unsignedByte
      value.to_i
  end
end

- (String) to_s

Returns the value as a string.

Returns:

  • (String)


322
323
324
# File 'lib/rdf/model/literal.rb', line 322

def to_s
  @object.to_s
end

- (Boolean) valid?

Returns true if the value adheres to the defined grammar of the datatype.

Returns:

Since:

  • 0.2.1



265
266
267
268
# File 'lib/rdf/model/literal.rb', line 265

def valid?
  grammar = self.class.const_get(:GRAMMAR) rescue nil
  grammar.nil? || !!(value =~ grammar)
end

- (RDF::Literal) validate! Also known as: validate

Validates the value using #valid?, raising an error if the value is invalid.

Returns:

Raises:

  • (ArgumentError)

    if the value is invalid

Since:

  • 0.2.1



287
288
289
290
# File 'lib/rdf/model/literal.rb', line 287

def validate!
  raise ArgumentError, "#{to_s.inspect} is not a valid <#{datatype.to_s}> literal" if invalid?
  self
end

- (String) value

Returns the value as a string.

Returns:

  • (String)


134
135
136
# File 'lib/rdf/model/literal.rb', line 134

def value
  @string || to_s
end