Skip to content

Commit bb94168

Browse files
committed
Finish 3.1.14
2 parents 455fa89 + e6f2255 commit bb94168

File tree

6 files changed

+71
-6
lines changed

6 files changed

+71
-6
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.13
1+
3.1.14

lib/rdf/model/literal.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,37 @@ def ==(other)
308308
end
309309
alias_method :===, :==
310310

311+
##
312+
# Compares `self` to `other` for sorting purposes (with type check).
313+
#
314+
# @param [Object] other
315+
# @return [Integer] `-1`, `0`, or `1`
316+
def <=>(other)
317+
case other
318+
when Literal
319+
case
320+
when self.eql?(other)
321+
0
322+
when self.language? && other.language?
323+
# Literals with languages can compare if languages are identical
324+
self.to_s <=> other.to_s
325+
when self.simple? && other.simple?
326+
self.to_s <=> other.to_s
327+
when !self.valid?
328+
type_error("#{self.inspect} is invalid") || 0
329+
when !other.valid?
330+
type_error("#{other.inspect} is invalid") || 0
331+
when self.comperable_datatype2?(other)
332+
self.object <=> other.object
333+
else
334+
type_error("#{self.inspect} and #{other.inspect} are not comperable") || 0
335+
end
336+
when String
337+
self.simple? && self.value <=> other
338+
else 1
339+
end
340+
end
341+
311342
##
312343
# Returns `true` if this is a plain literal. A plain literal
313344
# may have a language, but may not have a datatype. For
@@ -399,6 +430,32 @@ def comperable_datatype?(other)
399430
end
400431
end
401432

433+
##
434+
# Returns `true` if the literal has a datatype and the comparison should
435+
# return false instead of raise a type error.
436+
#
437+
# Used for <=> operator.
438+
#
439+
# This behavior is intuited from SPARQL data-r2/expr-equal/eq-2-2
440+
# @return [Boolean]
441+
def comperable_datatype2?(other)
442+
case self
443+
when RDF::Literal::Numeric, RDF::Literal::Boolean
444+
case other
445+
when RDF::Literal::Numeric, RDF::Literal::Boolean
446+
true
447+
else
448+
self.plain? || other.plain? ||
449+
self.language? || other.language? ||
450+
self.datatype == other.datatype
451+
end
452+
else
453+
self.plain? || other.plain? ||
454+
self.language? || other.language? ||
455+
self.datatype == other.datatype
456+
end
457+
end
458+
402459
##
403460
# Converts this literal into its canonical lexical representation.
404461
#

lib/rdf/model/literal/numeric.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class Numeric < Literal
1111
# @return [Integer] `-1`, `0`, or `1`
1212
# @since 0.3.0
1313
def <=>(other)
14+
# If lexically invalid, use regular literal testing
15+
return super unless self.valid? && (!other.respond_to?(:valid?) || other.valid?)
16+
1417
case other
1518
when ::Numeric
1619
to_d <=> other
@@ -30,11 +33,10 @@ def <=>(other)
3033
# @since 0.3.0
3134
def ==(other)
3235
# If lexically invalid, use regular literal testing
33-
return super unless self.valid?
36+
return super unless self.valid? && (!other.respond_to?(:valid?) || other.valid?)
3437

3538
case other
3639
when Literal::Numeric
37-
return super unless other.valid?
3840
(cmp = (self <=> other)) ? cmp.zero? : false
3941
when RDF::URI, RDF::Node
4042
# Interpreting SPARQL data-r2/expr-equal/eq-2-2, numeric can't be compared with other types

lib/rdf/model/statement.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def node?
301301
# @see RDF::Literal#==
302302
# @see RDF::Query::Variable#==
303303
def eql?(other)
304-
other.is_a?(Statement) && self == other && (self.graph_name || false) == (other.graph_name || false)
304+
other.is_a?(Statement) && self.to_a.eql?(other.to_a) && (self.graph_name || false) == (other.graph_name || false)
305305
end
306306

307307
##

lib/rdf/query/pattern.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def initialize!
5959
super
6060
end
6161

62+
##
63+
# Create a new pattern from the quads, recursivly dupping sub-patterns.
64+
def dup
65+
self.class.from(self.to_quad.map {|t| t.is_a?(RDF::Query::Pattern) ? t.dup : t})
66+
end
67+
6268
##
6369
# Any additional options for this pattern.
6470
#

spec/model_literal_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,9 +1454,9 @@ def self.literals(*selector)
14541454
"eq-2-2(bug) 'zzz'='zzz'^^<unknown>" => [RDF::Literal("zzz"), RDF::Literal("zzz", datatype: RDF::URI("unknown"))],
14551455
"numeric '1'=1" => [RDF::Literal("1"), RDF::Literal(1)],
14561456
"numeric 1='1'" => [RDF::Literal(1), RDF::Literal("1")],
1457-
"numeric 1=<xyz>" => [RDF::Literal(1), RDF::URI("xyz")], # From expr-equal/expr-2-2
1457+
"numeric 1=<xyz>" => [RDF::Literal(1), RDF::URI("http://example/xyz")], # From expr-equal/expr-2-2
14581458
"numeric 1=_:xyz" => [RDF::Literal(1), RDF::Node.new("xyz")], # From expr-equal/expr-2-2
1459-
"numeric <xyz>=1" => [RDF::URI("xyz"), RDF::Literal(1)], # From expr-equal/expr-2-2
1459+
"numeric <xyz>=1" => [RDF::URI("http://example/xyz"), RDF::Literal(1)], # From expr-equal/expr-2-2
14601460
"numeric _:xyz=1" => [RDF::Node.new("xyz"), RDF::Literal(1)], # From expr-equal/expr-2-2
14611461
"open-eq-04 'a'^^<unknown>=1" => [RDF::Literal.new("a", datatype: RDF::URI("unknown")), RDF::Literal(1)],
14621462
"open-eq-06 'b'^^<unknown>='a'^^<unknown>" => [RDF::Literal.new("b", datatype: RDF::URI("unknown")), RDF::Literal.new("a", datatype: RDF::URI("unknown"))],

0 commit comments

Comments
 (0)