Skip to content

Commit 340ab71

Browse files
committed
Update enabled methods defined for Query::Solution to include public_methods, and method`.
Note overridden instance methods and deprecate their usage.
1 parent 0fb0185 commit 340ab71

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

lib/rdf/query/solution.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ class RDF::Query
2323
class Solution
2424
# Undefine all superfluous instance methods:
2525
alias_method :__send, :send
26+
27+
# Temporarily remember instance method for deprecation message in `method_missing`.
28+
INSTANCE_METHODS = instance_methods
2629
undef_method(*instance_methods.
2730
map(&:to_s).
2831
select {|m| m.match?(/^\w+$/)}.
29-
reject {|m| %w(object_id dup instance_eval inspect to_s private_methods class should should_not pretty_print).include?(m) || m[0,2] == '__'}.
32+
reject {|m| %w(object_id dup instance_eval inspect to_s private_methods public_methods class method pretty_print).include?(m) || m[0,2] == '__'}.
3033
map(&:to_sym))
3134

3235
include Enumerable
@@ -344,6 +347,12 @@ def inspect
344347
# @return [RDF::Term]
345348
def method_missing(name, *args, &block)
346349
if args.empty? && @bindings.key?(name.to_sym)
350+
if INSTANCE_METHODS.include?(name)
351+
warn "[DEPRECATION] RDF::Query::Solution##{name} is an overridden instance method.\n" +
352+
"Its use as a solution accessor is deprecated and will be removed in a future version.\n" +
353+
"Use #[] for safe access.\n" +
354+
"Called from #{Gem.location_of_caller.join(':')}"
355+
end
347356
@bindings[name.to_sym]
348357
else
349358
super # raises NoMethodError

spec/query_solution_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@
99
end
1010
end
1111

12+
describe "accessors" do
13+
specify {expect(subject.a).to eq 1}
14+
specify {expect(subject[:a]).to eq 1}
15+
specify {expect(subject["a"]).to eq 1}
16+
specify {expect(subject["?b"]).to eq 2}
17+
specify {expect(subject["??c"]).to eq 3}
18+
specify {expect(subject["$d"]).to eq 4}
19+
specify {expect(subject["$$e"]).to eq 5}
20+
21+
context "with accessor overriding instance method" do
22+
subject {described_class.new(then: 'foo')}
23+
24+
it "notes deprecation when accessor is an overriddedn instance method" do
25+
expect do
26+
expect(subject.then).to eq 'foo'
27+
end.to write('[DEPRECATION]').to(:error)
28+
29+
expect do
30+
expect(subject[:then]).to eq 'foo'
31+
end.not_to write.to(:error)
32+
end
33+
end
34+
end
35+
1236
describe "#each_binding" do
1337
it "returns an enumerator" do
1438
expect(subject.each_binding).to be_an Enumerator

0 commit comments

Comments
 (0)