Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented an RDF::Literal::Token class for supporting xsd:token and…
… xsd:language literals.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |