mirror of
https://github.com/meineerde/rackstash.git
synced 2026-01-31 17:27:13 +00:00
Return an existing filter instance in Filters.build
This commit is contained in:
parent
8db2c917c2
commit
e857cdb9f1
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user