Skip to content

Commit

Permalink
close #98 by adding respond_to?
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Jan 3, 2014
1 parent dc5a2bb commit 90b106e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/active_interaction/modules/method_missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
module ActiveInteraction
# @private
module MethodMissing
def respond_to?(slug, include_all = false)
!!filter(slug) || super
end

def method_missing(slug, *args, &block)
begin
klass = Filter.factory(slug)
rescue MissingFilterError
super
end
super unless (klass = filter(slug))

options = args.last.is_a?(Hash) ? args.pop : {}

yield(klass, args, options) if block_given?

self
end

private

def filter(slug)
Filter.factory(slug)
rescue MissingFilterError
nil
end
end
end
18 changes: 18 additions & 0 deletions spec/active_interaction/modules/method_missing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
let(:klass) { Class.new { include ActiveInteraction::MethodMissing } }
subject(:instance) { klass.new }

describe '#respond_to?(slug, include_all = false)' do
context 'with invalid slug' do
let(:slug) { :slug }

it 'returns false' do
expect(instance.respond_to?(slug)).to be_false
end
end

context 'with valid slug' do
let(:slug) { :boolean }

it 'returns true' do
expect(instance.respond_to?(slug)).to be_true
end
end
end

describe '#method_missing' do
context 'with invalid slug' do
let(:slug) { :slug }
Expand Down

0 comments on commit 90b106e

Please sign in to comment.