Skip to content

Commit

Permalink
Implemented an RDF::Literal::Token class for supporting xsd:token and…
Browse files Browse the repository at this point in the history
… xsd:language literals.
  • Loading branch information
artob committed Aug 26, 2010
1 parent d250298 commit 64640e6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rdf/model/literal.rb
Expand Up @@ -46,6 +46,7 @@ class Literal
autoload :Date, 'rdf/model/literal/date'
autoload :DateTime, 'rdf/model/literal/datetime'
autoload :Time, 'rdf/model/literal/time'
autoload :Token, 'rdf/model/literal/token'
autoload :XML, 'rdf/model/literal/xml'

include RDF::Value
Expand Down Expand Up @@ -78,6 +79,8 @@ def self.new(value, options = {})
RDF::Literal::Integer
when XSD.unsignedLong, XSD.unsignedInt, XSD.unsignedShort, XSD.unsignedByte
RDF::Literal::Integer
when XSD.token, XSD.language
RDF::Literal::Token
when RDF.XMLLiteral
RDF::Literal::XML
else self
Expand All @@ -91,6 +94,7 @@ def self.new(value, options = {})
when ::DateTime then RDF::Literal::DateTime
when ::Date then RDF::Literal::Date
when ::Time then RDF::Literal::Time # FIXME: Ruby's Time class can represent datetimes as well
when ::Symbol then RDF::Literal::Token
else self
end
end
Expand Down
47 changes: 47 additions & 0 deletions lib/rdf/model/literal/token.rb
@@ -0,0 +1,47 @@
module RDF; class Literal
##
# A token literal.
#
# @see http://www.w3.org/TR/xmlschema-2/#token
# @since 0.2.3
class Token < Literal
DATATYPE = XSD.token
GRAMMAR = /\A[^\x0D\x0A\x09]+\z/i.freeze # FIXME

##
# @param [Symbol, #to_s] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
@datatype = RDF::URI(options[:datatype] || DATATYPE)
@string = options[:lexical] if options.has_key?(:lexical)
@string = value if !defined?(@string) && value.is_a?(String)
@object = value.is_a?(Symbol) ? value : value.to_s
end

##
# Converts the literal into its canonical lexical representation.
#
# @return [Literal]
# @see http://www.w3.org/TR/xmlschema-2/#boolean
def canonicalize
@string = @object.to_s if @object
self
end

##
# Returns the value as a symbol.
#
# @return [Symbol]
def to_sym
@object.to_sym
end

##
# Returns the value as a string.
#
# @return [String]
def to_s
@string || @object.to_s
end
end # class Token
end; end # class RDF::Literal

0 comments on commit 64640e6

Please sign in to comment.