1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-02-01 01:37:12 +00:00

Allow to pass initializer arguments to the encoder of a flow

This commit is contained in:
Holger Just 2018-07-18 12:41:25 +02:00
parent 9604ebf484
commit 9152b67df0
2 changed files with 18 additions and 6 deletions

View File

@ -178,17 +178,17 @@ module Rackstash
# Get or set the encoder for the log {#adapter}. If this value is not
# explicitly defined, it defaults to the #{adapter}'s default encoder.
#
# @param encoder [#encode, nil] if given, set the flow's encoder to this
# object
# @param encoder_spec (see Rackstash::Encoder.build)
# @param encoder_args (see Rackstash::Encoder.build)
# @param block (see Rackstash::Encoder.build)
# @raise [TypeError] if the given `encoder` does not respond to the `encode`
# method
# @return [#encode] the newly set encoder (if given) or the currently
# defined one
# @see #encoder=
def encoder(encoder = nil)
return @encoder if encoder.nil?
self.encoder = encoder
@encoder
def encoder(encoder_spec = nil, *encoder_args, &block)
return @encoder if encoder_spec.nil?
@encoder = Rackstash::Encoder.build(encoder_spec, *encoder_args, &block)
end
# Set the encoder for the log {#adapter}. You can use any object which

View File

@ -211,6 +211,18 @@ RSpec.describe Rackstash::Flow do
# The encoder is persisted and is returned afterwards
expect(flow.encoder).to equal encoder
end
it 'allows to set an encoder spec' do
expect { flow.encoder(:json) }.to change { flow.encoder }
.from(instance_of(Rackstash::Encoder::Raw))
.to(instance_of(Rackstash::Encoder::JSON))
end
it 'allows to set arguments to an encoder' do
flow.encoder(:message, tagged: ['tags'])
expect(flow.encoder).to be_instance_of(Rackstash::Encoder::Message)
expect(flow.encoder.tagged).to eql ['tags']
end
end
describe '#encoder=' do