1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-01-31 17:27:13 +00:00

Ensure only valid classes can be registered as filters and encoders

This commit is contained in:
Holger Just 2018-01-03 20:30:55 +01:00
parent fe44186da4
commit 65d75523c3
4 changed files with 71 additions and 12 deletions

View File

@ -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<String,Symbol>] 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) &&

View File

@ -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<String,Symbol>] 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

View File

@ -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

View File

@ -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