Class: RDF::Literal
- Inherits:
-
Object
- Object
- RDF::Literal
- 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.
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)
-
- (URI) datatype
The XML Schema datatype URI (optional).
-
- (Symbol) language
The language tag (optional).
Instance Method Summary (collapse)
-
- (Boolean) ==(other)
(also: #===)
Returns
trueif this literal is equivalent toother. -
- (Boolean) anonymous?
Returns
false. -
- (RDF::Literal) canonicalize
Returns a copy of this literal converted into its canonical lexical representation.
-
- (RDF::Literal) canonicalize!
Converts this literal into its canonical lexical representation.
-
- (Boolean) eql?(other)
Returns
trueif this literal is equal toother. -
- (Boolean) has_datatype?
(also: #datatype?, #datatyped?, #typed?)
Returns
trueif this is a datatyped literal. -
- (Boolean) has_language?
(also: #language?)
Returns
trueif this is a language-tagged literal. -
- (Fixnum) hash
Returns a hash code for this literal.
-
- (Literal) initialize(value, options = {})
constructor
A new instance of Literal.
-
- (String) inspect
Returns a developer-friendly representation of
self. -
- (Boolean) invalid?
Returns
trueif the value does not adhere to the defined grammar of the datatype. -
- (Boolean) literal?
Returns
true. - - (Object) object
-
- (String) to_s
Returns the value as a string.
-
- (Boolean) valid?
Returns
trueif the value adheres to the defined grammar of the datatype. -
- (RDF::Literal) validate!
(also: #validate)
Validates the value using #valid?, raising an error if the value is invalid.
-
- (String) value
Returns the value as a string.
Methods included from Term
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
123 124 125 126 127 128 |
# File 'lib/rdf/model/literal.rb', line 123 def initialize(value, = {}) @object = value @string = [:lexical] if [:lexical] @language = [:language].to_s.to_sym if [:language] @datatype = RDF::URI([:datatype]) if [:datatype] end |
Instance Attribute Details
- (URI) datatype
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
276 277 278 |
# File 'lib/rdf/model/literal.rb', line 276 def invalid? !valid? end |
- (Boolean) literal?
Returns true.
171 172 173 |
# File 'lib/rdf/model/literal.rb', line 171 def literal? true end |
- (Object) 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.
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.
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.
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.
134 135 136 |
# File 'lib/rdf/model/literal.rb', line 134 def value @string || to_s end |