diff --git a/lib/rackstash/encoder.rb b/lib/rackstash/encoder.rb index dc472be..405752e 100644 --- a/lib/rackstash/encoder.rb +++ b/lib/rackstash/encoder.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true # -# Copyright 2017 Holger Just +# Copyright 2017 - 2018 Holger Just # # This software may be modified and distributed under the terms # of the MIT license. See the LICENSE.txt file for details. @@ -31,12 +31,13 @@ module Rackstash # encoder object for it. # # @param encoder_class [Class] a class from which a new encoder can be - # created. Filter objects must respond to `encode` and accept an event - # hash. + # created. Encoder classes must implement the `encode` instance method + # which accepts an event hash and returns encoded event. # @param names [Array] one or more names for the # registered `encoder_class`. Using these names, the user can create a # new encoder object from the registered class in {.build}. - # @raise [TypeError] if objects of type were passed + # @raise [TypeError] if invalid arguments were passed, e.g. an unsuitable + # class or invalid names # @return [Class] the passed `filter_class` def register(encoder_class, *names) unless encoder_class.is_a?(Class) && diff --git a/lib/rackstash/filter.rb b/lib/rackstash/filter.rb index 6bde9be..e160980 100644 --- a/lib/rackstash/filter.rb +++ b/lib/rackstash/filter.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true # -# Copyright 2017 Holger Just +# Copyright 2017 - 2018 Holger Just # # This software may be modified and distributed under the terms # of the MIT license. See the LICENSE.txt file for details. @@ -29,14 +29,20 @@ module Rackstash # object for it. # # @param filter_class [Class] a class from which a new filter can be - # created. Filter objects must respond to `call` and accept an event - # hash. + # created. Filter classes must implement the `call` instance method + # which accepts an event hash. # @param filter_names [Array] one or more names for the # registered `filter_class`. Using these names, the user can create a # new filter object from the registered class in {.build}. - # @raise [TypeError] if objects of type were passed + # @raise [TypeError] if invalid arguments were passed, e.g. an unsuitable + # class or invalid names # @return [Class] the passed `filter_class` def register(filter_class, *filter_names) + unless filter_class.is_a?(Class) && + filter_class.instance_methods.include?(:call) + raise TypeError, 'Can only register filter classes' + end + filter_names.flatten.each do |name| registry[name] = filter_class end diff --git a/spec/rackstash/encoder_spec.rb b/spec/rackstash/encoder_spec.rb index 613a0a1..be319c9 100644 --- a/spec/rackstash/encoder_spec.rb +++ b/spec/rackstash/encoder_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true # -# Copyright 2017 Holger Just +# Copyright 2017 - 2018 Holger Just # # This software may be modified and distributed under the terms # of the MIT license. See the LICENSE.txt file for details. @@ -79,10 +79,36 @@ describe Rackstash::Encoder do end end - describe 'registry' do + describe '.registry' do it 'returns the encoder registry' do expect(described_class.registry).to be_instance_of Rackstash::ClassRegistry expect(described_class.registry.object_type).to eql 'encoder' end end + + describe '.register' do + let(:encoder_class) { + Class.new do + def encode; end + end + } + + it 'registers an encoder class' do + expect(described_class.registry).to receive(:[]=).with(:foo, encoder_class).ordered + expect(described_class.registry).to receive(:[]=).with(:bar, encoder_class).ordered + + described_class.register(encoder_class, :foo, :bar) + end + + it 'rejects invalid classes' do + expect(described_class.registry).not_to receive(:[]=) + + expect { described_class.register(:not_a_class, :foo) }.to raise_error TypeError + expect { described_class.register(Class.new, :foo) }.to raise_error TypeError + end + + it 'rejects invalid names' do + expect { described_class.register(encoder_class, 123) }.to raise_error TypeError + end + end end diff --git a/spec/rackstash/filter_spec.rb b/spec/rackstash/filter_spec.rb index a11557d..839d763 100644 --- a/spec/rackstash/filter_spec.rb +++ b/spec/rackstash/filter_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true # -# Copyright 2017 Holger Just +# Copyright 2017 - 2018 Holger Just # # This software may be modified and distributed under the terms # of the MIT license. See the LICENSE.txt file for details. @@ -150,10 +150,36 @@ describe Rackstash::Filter do end end - describe 'registry' do + describe '.registry' do it 'returns the filter registry' do expect(described_class.registry).to be_instance_of Rackstash::ClassRegistry expect(described_class.registry.object_type).to eql 'filter' end end + + describe '.register' do + let(:filter_class) { + Class.new do + def call; end + end + } + + it 'registers a filter class' do + expect(described_class.registry).to receive(:[]=).with(:foo, filter_class).ordered + expect(described_class.registry).to receive(:[]=).with(:bar, filter_class).ordered + + described_class.register(filter_class, :foo, :bar) + end + + it 'rejects invalid classes' do + expect(described_class.registry).not_to receive(:[]=) + + expect { described_class.register(:not_a_class, :foo) }.to raise_error TypeError + expect { described_class.register(Class.new, :foo) }.to raise_error TypeError + end + + it 'rejects invalid names' do + expect { described_class.register(filter_class, 123) }.to raise_error TypeError + end + end end