File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments