Kouhei Sutou
null+****@clear*****
Fri Jan 31 15:31:42 JST 2014
Kouhei Sutou 2014-01-31 15:31:42 +0900 (Fri, 31 Jan 2014) New Revision: 319ffbbf9f4ba76691be2bf20bfa5e8182ae4e66 https://github.com/droonga/fluent-plugin-droonga/commit/319ffbbf9f4ba76691be2bf20bfa5e8182ae4e66 Message: Add Command that has match pattern against message See comment about our small language for pattern. Added files: lib/droonga/command.rb test/unit/test_command.rb Added: lib/droonga/command.rb (+57 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/command.rb 2014-01-31 15:31:42 +0900 (f5c939d) @@ -0,0 +1,57 @@ +# Copyright (C) 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 +# 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 Command + attr_reader :method_name + # + # + # @option options [Array<Array>] :patterns The patterns to be matched + # against message. If all of the patterns are matched to a message, + # the command will be applied. + def initialize(method_name, options) + @method_name = method_name + @options = options + end + + def match?(message) + patterns.all? do |pattern| + match_pattern?(pattern, message) + end + end + + private + def patterns + @options[:patterns] || [] + end + + def match_pattern?(pattern, message) + path, operator, *arguments = pattern + target = path.split(".").inject(message) do |result, component| + result[component] + end + apply_operator(operator, target, arguments) + end + + def apply_operator(operator, target, arguments) + case operator + when :equal + [target] == arguments + else + raise InvalidArgument, "Unknown operator" + end + end + end +end Added: test/unit/test_command.rb (+55 -0) 100644 =================================================================== --- /dev/null +++ test/unit/test_command.rb 2014-01-31 15:31:42 +0900 (7bf328a) @@ -0,0 +1,55 @@ +# Copyright (C) 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 +# 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/command" + +class CommandTest < Test::Unit::TestCase + class MatchTest < self + def command(patterns) + Droonga::Command.new(:method_name, :patterns => patterns) + end + + def match?(patterns, message) + command(patterns).match?(message) + end + + class EqualTest < self + def test_top_level + assert_true(match?([["type", :equal, "select"]], + { + "type" => "select" + })) + end + + def test_nested + assert_true(match?([["body.output.limit", :equal, 10]], + { + "body" => { + "output" => { + "limit" => 10, + }, + }, + })) + end + + def test_different_value + assert_false(match?([["type", :equal, "select"]], + { + "type" => "search", + })) + end + end + end +end -------------- next part -------------- HTML����������������������������...Download