Skip to content

Commit 5a66f20

Browse files
authored
Merge pull request #337 from ruby-rdf/feature/implicit-conversions
Changes to implicit/explicit type conversions
2 parents d600322 + aadd63a commit 5a66f20

4 files changed

Lines changed: 93 additions & 6 deletions

File tree

lib/rdf/mixin/enumerator.rb

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,29 @@ class Enumerator < ::Enumerator
77
include Queryable
88
include Enumerable
99

10-
# Make sure returned arrays are also queryable
10+
##
11+
# @return [Array]
12+
# @note Make sure returned arrays are also queryable
1113
def to_a
1214
return super.to_a.extend(RDF::Queryable, RDF::Enumerable)
1315
end
14-
alias_method :to_ary, :to_a
16+
17+
protected
18+
19+
##
20+
# @overload #to_ary
21+
# @see #to_a
22+
# @deprecated use {#to_a} instead
23+
def method_missing(name, *args)
24+
if name == :to_ary
25+
warn "[DEPRECATION] #{self.class}#to_ary is deprecated, use " \
26+
"#{self.class}#to_a instead. Called from " \
27+
"#{Gem.location_of_caller.join(':')}"
28+
to_a
29+
else
30+
super
31+
end
32+
end
1533
end
1634
end
1735

@@ -28,11 +46,29 @@ class Enumerator < ::Enumerator
2846
include Queryable
2947
include Enumerable
3048

31-
# Make sure returned arrays are also queryable
49+
##
50+
# @return [Array]
51+
# @note Make sure returned arrays are also queryable
3252
def to_a
3353
return super.to_a.extend(RDF::Queryable, RDF::Enumerable)
3454
end
35-
alias_method :to_ary, :to_a
55+
56+
protected
57+
58+
##
59+
# @overload #to_ary
60+
# @see #to_a
61+
# @deprecated use {#to_a} instead
62+
def method_missing(name, *args)
63+
if name == :to_ary
64+
warn "[DEPRECATION] #{self.class}#to_ary is deprecated, use " \
65+
"#{self.class}#to_a instead. Called from " \
66+
"#{Gem.location_of_caller.join(':')}"
67+
self.to_a
68+
else
69+
super
70+
end
71+
end
3672
end
3773
end
3874
end

lib/rdf/model/literal.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,5 +477,27 @@ def humanize(lang = :en)
477477
def inspect
478478
sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, RDF::NTriples.serialize(self))
479479
end
480+
481+
protected
482+
483+
##
484+
# @overload #to_str
485+
# This method is implemented when the datatype is `xsd:string` or `rdf:langString`
486+
# @return [String]
487+
def method_missing(name, *args)
488+
case name
489+
when :to_str
490+
return to_s if @datatype == RDF.langString || @datatype == RDF::XSD.string
491+
end
492+
super
493+
end
494+
495+
def respond_to_missing?(name, include_private = false)
496+
case name
497+
when :to_str
498+
return true if @datatype == RDF.langString || @datatype == RDF::XSD.string
499+
end
500+
super
501+
end
480502
end # Literal
481503
end # RDF

lib/rdf/model/statement.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,14 @@ def to_quad
337337
def to_triple
338338
[subject, predicate, object]
339339
end
340+
alias_method :to_a, :to_triple
340341

341-
alias_method :to_a, :to_triple
342-
alias_method :to_ary, :to_triple
342+
##
343+
# @deprecated use {#to_a} or {#to_triple} instead
344+
# @see #to_triple
345+
def to_ary
346+
to_triple
347+
end
343348

344349
##
345350
# Canonicalizes each unfrozen term in the statement

spec/model_literal_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,30 @@ def self.literals(*selector)
221221
end
222222
end
223223

224+
# to_str is implemented for stringy xsd types, but not others
225+
describe "#to_str" do
226+
literals(:all_plain).each do |args|
227+
it "is implemented for #{args.inspect}" do
228+
expect(RDF::Literal.new(*args)).to respond_to :to_str
229+
end
230+
231+
it "matches #to_s for #{args.inspect}" do
232+
literal = RDF::Literal.new(*args)
233+
expect(literal.to_str).to eq literal.to_s
234+
end
235+
end
236+
237+
(literals(:all) - literals(:all_plain)).each do |args|
238+
it "is not implemented for #{args.inspect}" do
239+
expect(RDF::Literal.new(*args)).not_to respond_to :to_str
240+
end
241+
242+
it "raises NoMethodError for #{args.inspect}" do
243+
expect { RDF::Literal.new(*args).to_str }.to raise_error NoMethodError
244+
end
245+
end
246+
end
247+
224248
describe "#object" do
225249
literals(:all_plain).each do |args|
226250
it "returns value for #{args.inspect}" do

0 commit comments

Comments
 (0)