Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define method when hitting method missing #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/charlatan.rb
Expand Up @@ -45,15 +45,18 @@ def respond_to_missing?(method_name, include_private)

def method_missing(method_name, *args, &block)
if __proxy_target__.respond_to?(method_name)
response = __proxy_target__.public_send(method_name, *args, &block)

if response.equal?(__proxy_target__)
self
elsif response.kind_of?(__proxy_kind__)
self.class.new(*[response]+__proxy_args__)
else
response
self.class.send(:define_method, method_name) do |*options, &my_block|
response = __proxy_target__.public_send(method_name, *options, &my_block)

if response.equal?(__proxy_target__)
self
elsif response.kind_of?(__proxy_kind__)
self.class.new(*[response]+__proxy_args__)
else
response
end
end
send(method_name, *args, &block)
else
super
end
Expand Down
8 changes: 8 additions & 0 deletions spec/unit/charlatan_spec.rb
Expand Up @@ -73,6 +73,14 @@ def initialize(other, extra, &block)
expect(object.size).to be(3)
end

it 'will define the method after hitting method_missing' do
charlatan1 = Class.new { include Charlatan.new(:arr) }
object = charlatan1.new([])
object.should_receive(:method_missing).once.and_call_original
object.size
object.size
end

context 'when target kind is provided' do
subject(:charlatan) { klass.new(other_class.new) }

Expand Down