From e857cdb9f19dac952a2f12fa115bc64a17ac6597 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Fri, 20 Oct 2017 20:19:19 +0200 Subject: [PATCH] Return an existing filter instance in Filters.build --- lib/rackstash/filter_chain.rb | 4 +--- lib/rackstash/filters.rb | 14 +++++++++----- spec/rackstash/filters_spec.rb | 9 +++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/rackstash/filter_chain.rb b/lib/rackstash/filter_chain.rb index 5a1b216..8c54ed5 100644 --- a/lib/rackstash/filter_chain.rb +++ b/lib/rackstash/filter_chain.rb @@ -302,10 +302,8 @@ module Rackstash # @return [#call] a filter instance def build_filter(filter_spec, &block) if filter_spec.empty? - return block if block_given? + return Rackstash::Filters.build(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 diff --git a/lib/rackstash/filters.rb b/lib/rackstash/filters.rb index 1963300..7994b2c 100644 --- a/lib/rackstash/filters.rb +++ b/lib/rackstash/filters.rb @@ -34,11 +34,13 @@ module Rackstash # filter in which case we are resolving it to a class defined inside the # {Rackstash::Filters} namespace. # - # @param klass [Class, Symbol, String] a description of the class from which - # we are creating a new filter object. When giving a `Class`, we are using - # it as is. When giving a `String` or `Symbol`, we are determining the - # associated class from the {Rackstash::Filters} module and create an - # instance of that. + # @param klass [Class, Symbol, String, #call] a description of the class + # from which we are creating a new filter object. When giving a `Class`, + # we are using it as is. When giving a `String` or `Symbol`, we are + # determining the associated class from the {Rackstash::Filters} module + # and create an instance of that. When giving an object which responds to + # `call` already, we return it unchanged, ignoring any additional passed + # `args`. # @param args [Array] an optional list of arguments which is passed to the # initializer for the new filter object. # @raise [TypeError] if we can not create a new Filter object from `class` @@ -56,6 +58,8 @@ module Rackstash .to_sym filter_class = const_get(filter_class_name, false) filter_class.new(*args, &block) + when ->(filter) { filter.respond_to?(:call) } + klass else raise TypeError, "Can not build filter for #{klass.inspect}" end diff --git a/spec/rackstash/filters_spec.rb b/spec/rackstash/filters_spec.rb index 08a8e35..08457b7 100644 --- a/spec/rackstash/filters_spec.rb +++ b/spec/rackstash/filters_spec.rb @@ -43,10 +43,19 @@ describe Rackstash::Filters do described_class.build("filter_class#{random}", *args) end + it 'returns an existing filter' do + filter = -> {} + expect(described_class.build(filter)).to equal filter + expect(described_class.build(filter, :ignored, 42)).to equal filter + end + it 'raises a TypeError with different arguments' do expect { described_class.build(123) }.to raise_error(TypeError) expect { described_class.build(nil) }.to raise_error(TypeError) expect { described_class.build(true) }.to raise_error(TypeError) + + expect { described_class.build('MissingFilter') }.to raise_error(NameError) + expect { described_class.build(:missing_filter) }.to raise_error(NameError) end end