Kouhei Sutou
null+****@clear*****
Fri Nov 22 17:03:11 JST 2013
Kouhei Sutou 2013-11-22 17:03:11 +0900 (Fri, 22 Nov 2013) New Revision: a3a326edd495d0904062ef78adfcedd96ef24446 https://github.com/droonga/fluent-plugin-droonga/commit/a3a326edd495d0904062ef78adfcedd96ef24446 Message: Add PluginRepository It manages plugins. Added files: lib/droonga/plugin_repository.rb test/unit/test_plugin_repository.rb Added: lib/droonga/plugin_repository.rb (+46 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/plugin_repository.rb 2013-11-22 17:03:11 +0900 (816efc6) @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2013 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +module Droonga + class PluginRepository + include Enumerable + + def initialize + @plugins = {} + end + + def each(&block) + @plugins.each(&block) + end + + def register(name, klass) + @plugins[name] = klass + end + + def [](name) + @plugins[name] + end + + def clear + @plugins.clear + end + + def instantiate(name, *args, &block) + self[name].new(*args, &block) + end + end +end Added: test/unit/test_plugin_repository.rb (+89 -0) 100644 =================================================================== --- /dev/null +++ test/unit/test_plugin_repository.rb 2013-11-22 17:03:11 +0900 (9bcbee3) @@ -0,0 +1,89 @@ +# Copyright (C) 2013 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +require "droonga/plugin_repository" + +class PluginRepositoryTest < Test::Unit::TestCase + def setup + @repository = Droonga::PluginRepository.new + end + + class StubPlugin + attr_reader :arguments + def initialize(*arguments) + @arguments = arguments + end + end + + def test_register + @repository.register("stub", StubPlugin) + assert_equal(StubPlugin, @repository["stub"]) + end + + def test_enumerable + @repository.register("stub1", StubPlugin) + @repository.register("stub2", StubPlugin) + assert_equal([ + ["stub1", StubPlugin], + ["stub2", StubPlugin], + ], + @repository.to_a) + end + + sub_test_case("[]") do + def setup + super + @repository.register("stub", StubPlugin) + end + + def test_nonexistent + assert_nil(@repository["nonexistent"]) + end + + def test_existent + assert_equal(StubPlugin, @repository["stub"]) + end + end + + sub_test_case("clear") do + def setup + super + @repository.register("stub", StubPlugin) + end + + def test_clear + assert_equal([["stub", StubPlugin]], @repository.to_a) + @repository.clear + assert_equal([], @repository.to_a) + end + end + + sub_test_case("instantiate") do + def setup + super + @repository.register("stub", StubPlugin) + end + + def test_no_arguments + plugin =****@repos*****("stub") + assert_equal([], plugin.arguments) + end + + def test_have_arguments + plugin =****@repos*****("stub", "Hello", "World") + assert_equal(["Hello", "World"], plugin.arguments) + end + end +end -------------- next part -------------- HTML����������������������������...Download