Skip to content

Commit

Permalink
fix: add missed definition for Slaver::Proxy#respond_to_missing?
Browse files Browse the repository at this point in the history
Slaver::Proxy#respond_to? now will check if Slaver::Proxy#safe_connection
could respond to specified method.
  • Loading branch information
ZhidkovDenis committed Dec 16, 2019
1 parent 29081ac commit e842c9f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/slaver/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ def safe_connection
def method_missing(method, *args, &block)
safe_connection.send(method, *args, &block)
end

def respond_to_missing?(method, include_private = false)
safe_connection.respond_to?(method, include_private) || super
end
end
end
27 changes: 27 additions & 0 deletions spec/lib/slaver/proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe Slaver::Proxy do
describe 'connection methods delegation' do
context 'when proxy connection does not respond to called method' do
it 'does not respond and raises NameError' do
expect(Foo.on(:other).connection.respond_to?(:not_existed_method)).to eq(false)
expect { Foo.on(:other).connection.method(:not_existed_method) }.to raise_error(NameError)
expect { Foo.on(:other).connection.not_existed_method }.to raise_error(NameError)
end
end

context 'when proxy connection responds to called method' do
let(:query) { "SELECT COUNT(*) FROM #{Foo.quoted_table_name} WHERE name = 'other_1'" }

it 'responds to method by calling it on proxy connection' do
Foo.on(:other).create!(name: 'other_1')
Foo.on(:other).create!(name: 'other_2')

expect(Foo.on(:other).connection.respond_to?(:select_value)).to eq(true)
expect(Foo.on(:other).connection.method(:select_value)).to be
expect(Foo.on(:other).connection.select_value(query).to_i).to eq(1)
expect(Foo.connection.select_value(query).to_i).to eq(0)
end
end
end
end

0 comments on commit e842c9f

Please sign in to comment.