Kouhei Sutou
null+****@clear*****
Fri Jan 31 15:52:47 JST 2014
Kouhei Sutou 2014-01-31 15:52:47 +0900 (Fri, 31 Jan 2014) New Revision: e281dc2d4b394a039769dc640088cf83a010a918 https://github.com/droonga/fluent-plugin-droonga/commit/e281dc2d4b394a039769dc640088cf83a010a918 Message: Use Command when registering a command in plugin API is still compatible. Modified files: lib/droonga/plugin_registerable.rb Renamed files: lib/droonga/command_repository.rb (from lib/droonga/command_mapper.rb) test/unit/test_command_repository.rb (from test/unit/test_command_mapper.rb) Renamed: lib/droonga/command_repository.rb (+9 -20) 59% =================================================================== --- lib/droonga/command_mapper.rb 2014-01-31 15:32:38 +0900 (cdc6cfb) +++ lib/droonga/command_repository.rb 2014-01-31 15:52:47 +0900 (ebe8764) @@ -1,4 +1,4 @@ -# Copyright (C) 2013 Droonga Project +# Copyright (C) 2013-2014 Droonga Project # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -14,30 +14,19 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA module Droonga - class CommandMapper + class CommandRepository def initialize - @commands = {} + @commands = [] end - def register(name_or_map) - if name_or_map.is_a?(Hash) - command_map = name_or_map - command_map.each do |command_name, method_name| - @commands[command_name.to_s] = method_name - end - else - name = name_or_map - method_name = name - @commands[name.to_s] = method_name - end - end - - def [](command) - @commands[command.to_s] + def register(command) + @commands << command end - def commands - @commands.keys + def find(message) + @commands.find do |command| + command.match?(message) + end end end end Modified: lib/droonga/plugin_registerable.rb (+31 -8) =================================================================== --- lib/droonga/plugin_registerable.rb 2014-01-31 15:32:38 +0900 (aa3b47d) +++ lib/droonga/plugin_registerable.rb 2014-01-31 15:52:47 +0900 (57490ab) @@ -15,7 +15,8 @@ # 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/command_mapper" +require "droonga/command" +require "droonga/command_repository" require "droonga/plugin_repository" module Droonga @@ -33,19 +34,41 @@ module Droonga def inherited(sub_class) super - sub_class.instance_variable_set(:@command_mapper, CommandMapper.new) + sub_class.instance_variable_set(:@command_repository, + CommandRepository.new) end - def command(name_or_map) - @command_mapper.register(name_or_map) + def command(method_name_or_map, options={}) + if method_name_or_map.is_a?(Hash) + method_name, type = method_name_or_map.to_a.first + options[:patterns] ||= [] + options[:patterns] << ["type", :equal, type.to_s] + else + method_name = method_name_or_map + if options.empty? + options[:patterns] ||= [] + options[:patterns] << ["type", :equal, method_name.to_s] + end + end + command = Command.new(method_name, options) + @command_repository.register(command) + end + + def find_command(message) + @command_repository.find(message) end - def method_name(command) - @command_mapper[command] + def method_name(message) + message = {"type" => message.to_s} unless message.is_a?(Hash) + command = find_command(message) + return nil if command.nil? + command.method_name end - def processable?(command) - not method_name(command).nil? + def processable?(message) + message = {"type" => message.to_s} unless message.is_a?(Hash) + command = find_command(message) + not command.nil? end end end Renamed: test/unit/test_command_repository.rb (+15 -20) 51% =================================================================== --- test/unit/test_command_mapper.rb 2014-01-31 15:32:38 +0900 (0a95d0e) +++ test/unit/test_command_repository.rb 2014-01-31 15:52:47 +0900 (e14113a) @@ -13,32 +13,27 @@ # 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/command_mapper" +require "droonga/command_repository" -class CommandMapperTest < Test::Unit::TestCase - class RegisterTest < self - def setup - @command_mapper = Droonga::CommandMapper.new - end +class CommandRepositoryTest < Test::Unit::TestCase + def setup + @repository = Droonga::CommandRepository.new + end - def test_name - @command_mapper.register(:select) - assert_equal(:select, @command_mapper[:select]) + class FindTest < self + def setup + super + @command = Droonga::Command.new(:select, + :patterns => [["type", :equal, "select"]]) + @repository.register(@command) end - def test_different_method_name - @command_mapper.register(:command_name => :method_name) - assert_equal(:method_name, @command_mapper[:command_name]) + def test_match + assert_equal(@command, @repository.find({ "type" => "select" })) end - def test_multiple_pairs - map = { - :command_name_1 => :command_name_1, - :command_name_2 => :command_name_2, - } - @command_mapper.register(map) - assert_equal(["command_name_1", "command_name_2"], - @command_mapper.commands) + def test_not_match + assert_nil(@repository.find({ "type" => "search" })) end end end -------------- next part -------------- HTML����������������������������...Download