diff --git a/lib/rackstash/filter_chain.rb b/lib/rackstash/filter_chain.rb index 3eca5c0..07fe27b 100644 --- a/lib/rackstash/filter_chain.rb +++ b/lib/rackstash/filter_chain.rb @@ -63,7 +63,7 @@ module Rackstash # @param filter [#call, nil] the filter to set at `index` # @return [#call] the given `filter` def []=(index, filter) - filter = build_filter(filter) + raise TypeError, 'must provide a filter' unless filter.respond_to?(:call) synchronize do id = index_at(index) @@ -85,8 +85,8 @@ module Rackstash # which we will then take as the filter. # @raise [TypeError] if the given filter is not callable # @return [self] - def append(filter = nil, &block) - filter = build_filter(filter || block) + def append(*filter_spec, &block) + filter = build_filter(filter_spec, &block) synchronize do @filters.push filter @@ -178,8 +178,8 @@ module Rackstash # @raise [TypeError] if the given filter is not callable # @raise [ArgumentError] if the existing filter could not be found # @return [self] - def insert_after(index, filter = nil, &block) - filter = build_filter(filter || block) + def insert_after(index, *filter_spec, &block) + filter = build_filter(filter_spec, &block) synchronize do id = index_at(index) @@ -207,8 +207,8 @@ module Rackstash # @raise [TypeError] if the given filter is not callable # @raise [ArgumentError] if the existing filter could not be found # @return [self] - def insert_before(index, filter = nil, &block) - filter = build_filter(filter || block) + def insert_before(index, *filter_spec, &block) + filter = build_filter(filter_spec, &block) synchronize do id = index_at(index) @@ -246,8 +246,8 @@ module Rackstash # which we will then take as the filter. # @raise [TypeError] if the given filter is not callable # @return [self] - def unshift(filter = nil, &block) - filter = build_filter(filter || block) + def unshift(*filter_spec, &block) + filter = build_filter(filter_spec, &block) synchronize do @filters.unshift filter @@ -284,18 +284,23 @@ module Rackstash index.to_int when Class @filters.index { |filter| filter.is_a?(index) } - when String, ->(o) { o.respond_to?(:to_str) } - index = index.to_str + when Symbol, String + index = index.to_s @filters.index { |filter| filter.class.ancestors.map(&:name).include?(index) } else @filters.index { |filter| filter == index } end end - def build_filter(filter) - raise TypeError, 'must provide a filter' unless filter.respond_to?(:call) - - filter + def build_filter(filter_spec, &block) + if filter_spec.empty? + return block if block_given? + raise ArgumentError, 'Need to specify a filter' + elsif filter_spec.length == 1 && filter_spec.first.respond_to?(:call) + filter_spec.first + else + Rackstash::Filters.build(*filter_spec, &block) + end end end end diff --git a/lib/rackstash/flow.rb b/lib/rackstash/flow.rb index 8ca334a..0d38c6b 100644 --- a/lib/rackstash/flow.rb +++ b/lib/rackstash/flow.rb @@ -142,14 +142,14 @@ module Rackstash end # (see FilterChain#insert_after) - def filter_after(index, filter = nil, &block) - @filter_chain.insert_after(index, filter, &block) + def filter_after(index, *filter, &block) + @filter_chain.insert_after(index, *filter, &block) self end # (see FilterChain#append) - def filter_append(filter = nil, &block) - @filter_chain.append(filter, &block) + def filter_append(*filter, &block) + @filter_chain.append(*filter, &block) self end alias filter filter_append @@ -160,14 +160,14 @@ module Rackstash end # (see FilterChain#insert_before) - def filter_before(index, filter = nil, &block) - @filter_chain.insert_before(index, filter, &block) + def filter_before(index, *filter, &block) + @filter_chain.insert_before(index, *filter, &block) self end # (see FilterChain#unshift) - def filter_prepend(filter = nil, &block) - @filter_chain.unshift(filter, &block) + def filter_prepend(*filter, &block) + @filter_chain.unshift(*filter, &block) self end alias filter_unshift filter_prepend diff --git a/spec/rackstash/filter_chain_spec.rb b/spec/rackstash/filter_chain_spec.rb index b12cfa7..9d17f80 100644 --- a/spec/rackstash/filter_chain_spec.rb +++ b/spec/rackstash/filter_chain_spec.rb @@ -117,7 +117,7 @@ describe Rackstash::FilterChain do expect { filter_chain[34] = new_filter }.to raise_error ArgumentError end - it 'raises a TypeError if the object is not a filter' do + it 'raises an error if the object is not a filter' do expect { filter_chain[0] = :foo }.to raise_error TypeError expect { filter_chain[0] = nil }.to raise_error TypeError expect { filter_chain[0] = false }.to raise_error TypeError @@ -137,12 +137,15 @@ describe Rackstash::FilterChain do expect(filter_chain[0]).to be_instance_of(Proc) end - it 'raises a TypeError if the object is not a filter' do - expect { filter_chain.append(:foo) }.to raise_error TypeError + it 'raises an error if the object is not a filter' do expect { filter_chain.append(nil) }.to raise_error TypeError expect { filter_chain.append(false) }.to raise_error TypeError expect { filter_chain.append(42) }.to raise_error TypeError - expect { filter_chain.append('Foo') }.to raise_error TypeError + + expect { filter_chain.append(:foo) }.to raise_error NameError + expect { filter_chain.append('Foo') }.to raise_error NameError + + expect { filter_chain.append }.to raise_error ArgumentError end it 'can use #<< alias' do @@ -329,11 +332,14 @@ describe Rackstash::FilterChain do end it 'raises a TypeError if the object is not a filter' do - expect { filter_chain.insert_before(1, :foo) }.to raise_error TypeError expect { filter_chain.insert_before(1, nil) }.to raise_error TypeError expect { filter_chain.insert_before(1, false) }.to raise_error TypeError expect { filter_chain.insert_before(1, 42) }.to raise_error TypeError - expect { filter_chain.insert_before(1, 'Foo') }.to raise_error TypeError + + expect { filter_chain.insert_before(1, :foo) }.to raise_error NameError + expect { filter_chain.insert_before(1, 'Foo') }.to raise_error NameError + + expect { filter_chain.insert_before(1) }.to raise_error ArgumentError end end @@ -382,12 +388,15 @@ describe Rackstash::FilterChain do expect { filter_chain.insert_after(Class.new, inserted) }.to raise_error ArgumentError end - it 'raises a TypeError if the object is not a filter' do - expect { filter_chain.insert_after(1, :foo) }.to raise_error TypeError + it 'raises an error if the object is not a filter' do expect { filter_chain.insert_after(1, nil) }.to raise_error TypeError expect { filter_chain.insert_after(1, false) }.to raise_error TypeError expect { filter_chain.insert_after(1, 42) }.to raise_error TypeError - expect { filter_chain.insert_after(1, 'Foo') }.to raise_error TypeError + + expect { filter_chain.insert_after(1, :foo) }.to raise_error NameError + expect { filter_chain.insert_after(1, 'Foo') }.to raise_error NameError + + expect { filter_chain.insert_after(1) }.to raise_error ArgumentError end end @@ -436,12 +445,15 @@ describe Rackstash::FilterChain do expect(filter_chain.size).to eql 2 end - it 'raises a TypeError if the object is not a filter' do - expect { filter_chain.unshift(:foo) }.to raise_error TypeError + it 'raises an error if the object is not a filter' do expect { filter_chain.unshift(nil) }.to raise_error TypeError expect { filter_chain.unshift(false) }.to raise_error TypeError expect { filter_chain.unshift(42) }.to raise_error TypeError - expect { filter_chain.unshift('Foo') }.to raise_error TypeError + + expect { filter_chain.unshift(:foo) }.to raise_error NameError + expect { filter_chain.unshift('Foo') }.to raise_error NameError + + expect { filter_chain.unshift }.to raise_error ArgumentError end end