Skip to content

Commit

Permalink
Changed RDF::Literal#canonicalize to be non-mutative, and added a new…
Browse files Browse the repository at this point in the history
… mutative RDF::Literal#canonicalize! method.

RDF::Literal#canonicalize now returns a canonicalized copy of the literal, instead of mutatively canonicalizing the receiver.

RDF::Literal#canonicalize! is now the method that datatype-specific RDF::Literal subclasses should override from RDF.rb 0.3.0 onwards.
  • Loading branch information
artob committed Nov 15, 2010
1 parent 4ec6624 commit 2de3ddf
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 56 deletions.
35 changes: 24 additions & 11 deletions lib/rdf/model/literal.rb
Expand Up @@ -100,8 +100,8 @@ def self.new(value, options = {})
end
literal = klass.allocate
literal.send(:initialize, value, options)
literal.validate if options[:validate]
literal.canonicalize if options[:canonicalize]
literal.validate! if options[:validate]
literal.canonicalize! if options[:canonicalize]
literal
end

Expand Down Expand Up @@ -263,23 +263,36 @@ def invalid?
# Validates the value using {#valid?}, raising an error if the value is
# invalid.
#
# @return [RDF::Literal] `self`
# @raise [ArgumentError] if the value is invalid
# @return [Literal]
# @since 0.2.1
def validate
raise ArgumentError.new("#{to_s.inspect} is not a valid <#{datatype.to_s}> literal") if invalid?
def validate!
raise ArgumentError, "#{to_s.inspect} is not a valid <#{datatype.to_s}> literal" if invalid?
self
end
alias_method :validate!, :validate
alias_method :validate, :validate!

##
# Converts the literal into its canonical lexical representation.
# Returns a copy of this literal converted into its canonical lexical
# representation.
#
# Subclasses should override this as needed and appropriate.
# Subclasses should override `#canonicalize!` as needed and appropriate,
# not this method.
#
# @return [Literal]
# @return [RDF::Literal]
# @since 0.2.1
def canonicalize
self.dup.canonicalize!
end

##
# Converts this literal into its canonical lexical representation.
#
# Subclasses should override this as needed and appropriate.
#
# @return [RDF::Literal] `self`
# @since 0.3.0
def canonicalize!
@language = @language.to_s.downcase.to_sym if @language
self
end
Expand All @@ -299,5 +312,5 @@ def to_s
def inspect
sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, RDF::NTriples.serialize(self))
end
end
end
end # Literal
end # RDF
10 changes: 5 additions & 5 deletions lib/rdf/model/literal/boolean.rb
Expand Up @@ -27,11 +27,11 @@ def initialize(value, options = {})
end

##
# Converts the literal into its canonical lexical representation.
# Converts this literal into its canonical lexical representation.
#
# @return [Literal]
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema-2/#boolean-canonical-representation
def canonicalize
def canonicalize!
@string = (@object ? :true : :false).to_s
self
end
Expand Down Expand Up @@ -95,5 +95,5 @@ def true?
def false?
@object.equal?(false)
end
end # class Boolean
end; end # class RDF::Literal
end # Boolean
end; end # RDF::Literal
10 changes: 5 additions & 5 deletions lib/rdf/model/literal/date.rb
Expand Up @@ -23,11 +23,11 @@ def initialize(value, options = {})
end

##
# Converts the literal into its canonical lexical representation.
# Converts this literal into its canonical lexical representation.
#
# @return [Literal]
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema-2/#date
def canonicalize
def canonicalize!
@string = @object.strftime('%Y-%m-%d%Z').sub(/\+00:00|UTC/, 'Z')
self
end
Expand All @@ -51,5 +51,5 @@ def valid?
def to_s
@string || @object.strftime('%Y-%m-%d%Z').sub(/\+00:00|UTC/, 'Z')
end
end # class Date
end; end # class RDF::Literal
end # Date
end; end # RDF::Literal
10 changes: 5 additions & 5 deletions lib/rdf/model/literal/datetime.rb
Expand Up @@ -23,11 +23,11 @@ def initialize(value, options = {})
end

##
# Converts the literal into its canonical lexical representation.
# Converts this literal into its canonical lexical representation.
#
# @return [Literal]
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema-2/#dateTime
def canonicalize
def canonicalize!
@string = @object.new_offset(0).strftime('%Y-%m-%dT%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
self
end
Expand All @@ -51,5 +51,5 @@ def valid?
def to_s
@string || @object.strftime('%Y-%m-%dT%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
end
end # class DateTime
end; end # class RDF::Literal
end # DateTime
end; end # RDF::Literal
10 changes: 5 additions & 5 deletions lib/rdf/model/literal/decimal.rb
Expand Up @@ -28,11 +28,11 @@ def initialize(value, options = {})
end

##
# Converts the literal into its canonical lexical representation.
# Converts this literal into its canonical lexical representation.
#
# @return [Literal]
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema-2/#decimal
def canonicalize
def canonicalize!
# Can't use simple %f transformation due to special requirements from
# N3 tests in representation
@string = begin
Expand Down Expand Up @@ -204,5 +204,5 @@ def to_d
def to_r
@object.to_r # only available on Ruby 1.9+
end
end # class Decimal
end; end # class RDF::Literal
end # Decimal
end; end # RDF::Literal
10 changes: 5 additions & 5 deletions lib/rdf/model/literal/double.rb
Expand Up @@ -30,11 +30,11 @@ def initialize(value, options = {})
end

##
# Converts the literal into its canonical lexical representation.
# Converts this literal into its canonical lexical representation.
#
# @return [Literal]
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema-2/#double
def canonicalize
def canonicalize!
# Can't use simple %f transformation due to special requirements from
# N3 tests in representation
@string = case
Expand Down Expand Up @@ -280,5 +280,5 @@ def to_d
def to_r
@object.to_r # only available on Ruby 1.9+
end
end # class Double
end; end # class RDF::Literal
end # Double
end; end # RDF::Literal
10 changes: 5 additions & 5 deletions lib/rdf/model/literal/integer.rb
Expand Up @@ -30,11 +30,11 @@ def initialize(value, options = {})
end

##
# Converts the literal into its canonical lexical representation.
# Converts this literal into its canonical lexical representation.
#
# @return [Literal]
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema-2/#integer
def canonicalize
def canonicalize!
@string = @object.to_s if @object
self
end
Expand Down Expand Up @@ -214,5 +214,5 @@ def to_bn
require 'openssl' unless defined?(OpenSSL::BN)
OpenSSL::BN.new(to_s)
end
end # class Integer
end; end # class RDF::Literal
end # Integer
end; end # RDF::Literal
10 changes: 5 additions & 5 deletions lib/rdf/model/literal/time.rb
Expand Up @@ -27,7 +27,7 @@ def initialize(value, options = {})
end

##
# Converts the literal into its canonical lexical representation.
# Converts this literal into its canonical lexical representation.
#
# §3.2.8.2 Canonical representation
#
Expand All @@ -37,9 +37,9 @@ def initialize(value, options = {})
# time zone must be Coordinated Universal Time (UTC) indicated by a "Z".
# Additionally, the canonical representation for midnight is 00:00:00.
#
# @return [Literal]
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema-2/#time
def canonicalize
def canonicalize!
@string = @object.utc.strftime('%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
self
end
Expand All @@ -51,5 +51,5 @@ def canonicalize
def to_s
@string || @object.strftime('%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
end
end # class Time
end; end # class RDF::Literal
end # Time
end; end # RDF::Literal
10 changes: 5 additions & 5 deletions lib/rdf/model/literal/token.rb
Expand Up @@ -19,11 +19,11 @@ def initialize(value, options = {})
end

##
# Converts the literal into its canonical lexical representation.
# Converts this literal into its canonical lexical representation.
#
# @return [Literal]
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema-2/#boolean
def canonicalize
def canonicalize!
@string = @object.to_s if @object
self
end
Expand All @@ -43,5 +43,5 @@ def to_sym
def to_s
@string || @object.to_s
end
end # class Token
end; end # class RDF::Literal
end # Token
end; end # RDF::Literal
10 changes: 5 additions & 5 deletions lib/rdf/model/literal/xml.rb
Expand Up @@ -19,11 +19,11 @@ def initialize(value, options = {})
end

##
# Converts the literal into its canonical lexical representation.
# Converts this literal into its canonical lexical representation.
#
# @return [Literal]
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xml-exc-c14n/
def canonicalize
def canonicalize!
# TODO: implement XML canonicalization
self
end
Expand All @@ -35,5 +35,5 @@ def canonicalize
def to_s
@string || @object.to_s # TODO
end
end # class XML
end; end # class RDF::Literal
end # XML
end; end # RDF::Literal

0 comments on commit 2de3ddf

Please sign in to comment.