Kouhei Sutou
null+****@clear*****
Fri Feb 7 11:45:33 JST 2014
Kouhei Sutou 2014-02-07 11:45:33 +0900 (Fri, 07 Feb 2014) New Revision: 21139d1c4f8e9e17a16f0c4b450484edb368e7d8 https://github.com/droonga/fluent-plugin-droonga/commit/21139d1c4f8e9e17a16f0c4b450484edb368e7d8 Message: Extract MessageMatcher from Command Copied files: lib/droonga/message_matcher.rb (from lib/droonga/command.rb) Modified files: lib/droonga/command.rb Renamed files: test/unit/test_message_matcher.rb (from test/unit/test_command.rb) Modified: lib/droonga/command.rb (+5 -79) =================================================================== --- lib/droonga/command.rb 2014-02-07 11:28:32 +0900 (ef38d77) +++ lib/droonga/command.rb 2014-02-07 11:45:33 +0900 (684c866) @@ -13,98 +13,24 @@ # 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/message_matcher" + module Droonga class Command attr_reader :method_name - # - # # @option options [Array] :pattern The pattern to be matched # against message. If the pattern is matched to a message, # the command will be applied. # - # Here is pattern syntax. - # - # * PATTERN = [TARGET_PATH, OPERATOR, ARGUMENTS*] - # * PATTERN = [PATTERN, LOGICAL_OPERATOR, PATTERN] - # * TARGET_PATH = "COMPONENT(.COMPONENT)*" - # * OPERATOR = :equal, :in, :include?, :exist? - # (More operators may be added in the future. - # For example, :start_with and so on.) - # * ARGUMENTS = OBJECT_DEFINED_IN_JSON* - # * LOGICAL_OPERATOR = :or (:add will be added.) - # - # For example: - # - # ``` - # ["type", :equal, "search"] - # ``` - # - # matches to the following message: - # - # ``` - # {"type" => "search"} - # ``` - # - # Another example: - # - # ``` - # ["body.output.limit", :equal, 10] - # ``` - # - # matches to the following message: - # - # ``` - # { - # "body" => { - # "output" => { - # "limit" => 10, - # }, - # }, - # } - # ``` + # @see MessageMatcher def initialize(method_name, options) @method_name = method_name @options = options + @matcher = MessageMatcher.new(@options[:pattern]) end def match?(message) - match_pattern?(@options[:pattern], message) - end - - private - def match_pattern?(pattern, message) - return false if pattern.nil? - path, operator, *arguments = pattern - target = resolve_path(path, message) - apply_operator(operator, target, arguments) - end - - NONEXISTENT_PATH = Object.new - def resolve_path(path, message) - path.split(".").inject(message) do |result, component| - return NONEXISTENT_PATH unless result.is_a?(Hash) - result[component] - end - end - - def apply_operator(operator, target, arguments) - case operator - when :equal - [target] == arguments - when :in - arguments.any? do |argument| - argument.include?(target) - end - when :include? - return false unless target.respond_to?(:include?) - arguments.any? do |argument| - target.include?(argument) - end - when :exist? - target != NONEXISTENT_PATH - else - raise ArgumentError, "Unknown operator" - end + @matcher.match?(message) end end end Copied: lib/droonga/message_matcher.rb (+49 -58) 51% =================================================================== --- lib/droonga/command.rb 2014-02-07 11:28:32 +0900 (ef38d77) +++ lib/droonga/message_matcher.rb 2014-02-07 11:45:33 +0900 (fa4907d) @@ -14,71 +14,62 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA module Droonga - class Command - attr_reader :method_name - # - # - # @option options [Array] :pattern The pattern to be matched - # against message. If the pattern is matched to a message, - # the command will be applied. - # - # Here is pattern syntax. - # - # * PATTERN = [TARGET_PATH, OPERATOR, ARGUMENTS*] - # * PATTERN = [PATTERN, LOGICAL_OPERATOR, PATTERN] - # * TARGET_PATH = "COMPONENT(.COMPONENT)*" - # * OPERATOR = :equal, :in, :include?, :exist? - # (More operators may be added in the future. - # For example, :start_with and so on.) - # * ARGUMENTS = OBJECT_DEFINED_IN_JSON* - # * LOGICAL_OPERATOR = :or (:add will be added.) - # - # For example: - # - # ``` - # ["type", :equal, "search"] - # ``` - # - # matches to the following message: - # - # ``` - # {"type" => "search"} - # ``` - # - # Another example: - # - # ``` - # ["body.output.limit", :equal, 10] - # ``` - # - # matches to the following message: - # - # ``` - # { - # "body" => { - # "output" => { - # "limit" => 10, - # }, - # }, - # } - # ``` - def initialize(method_name, options) - @method_name = method_name - @options = options + # It checks whether the pattern matches against a message. + # + # It provides the small language. Here is the pattern syntax. + # + # * PATTERN = [TARGET_PATH, OPERATOR, ARGUMENTS*] + # * PATTERN = [PATTERN, LOGICAL_OPERATOR, PATTERN] + # * TARGET_PATH = "COMPONENT(.COMPONENT)*" + # * OPERATOR = :equal, :in, :include?, :exist? + # (More operators may be added in the future. + # For example, :start_with and so on.) + # * ARGUMENTS = OBJECT_DEFINED_IN_JSON* + # * LOGICAL_OPERATOR = :or (:add will be added.) + # + # For example: + # + # ``` + # ["type", :equal, "search"] + # ``` + # + # matches to the following message: + # + # ``` + # {"type" => "search"} + # ``` + # + # Another example: + # + # ``` + # ["body.output.limit", :equal, 10] + # ``` + # + # matches to the following message: + # + # ``` + # { + # "body" => { + # "output" => { + # "limit" => 10, + # }, + # }, + # } + # ``` + class MessageMatcher + # @param [Array] pattern The pattern to be matched against a message. + def initialize(pattern) + @pattern = pattern end def match?(message) - match_pattern?(@options[:pattern], message) - end - - private - def match_pattern?(pattern, message) - return false if pattern.nil? - path, operator, *arguments = pattern + return false if****@patte*****? + path, operator, *arguments = @pattern target = resolve_path(path, message) apply_operator(operator, target, arguments) end + private NONEXISTENT_PATH = Object.new def resolve_path(path, message) path.split(".").inject(message) do |result, component| Renamed: test/unit/test_message_matcher.rb (+7 -15) 90% =================================================================== --- test/unit/test_command.rb 2014-02-07 11:28:32 +0900 (ab6ad26) +++ test/unit/test_message_matcher.rb 2014-02-07 11:45:33 +0900 (638927e) @@ -13,24 +13,20 @@ # 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" +require "droonga/message_matcher" -class CommandTest < Test::Unit::TestCase - def command(method_name, options={}) - Droonga::Command.new(method_name, options) +class MessageMatcherTest < Test::Unit::TestCase + def matcher(pattern) + Droonga::MessageMatcher.new(pattern) end class ResolvePathTest < self - def command - super(:method_name) - end - def resolve_path(path, message) - command.send(:resolve_path, path, message) + matcher(nil).send(:resolve_path, path, message) end def test_nonexistent - assert_equal(Droonga::Command::NONEXISTENT_PATH, + assert_equal(Droonga::MessageMatcher::NONEXISTENT_PATH, resolve_path("nonexistent.path", {})) end @@ -56,12 +52,8 @@ class CommandTest < Test::Unit::TestCase end class MatchTest < self - def command(pattern) - super(:method_name, :pattern => pattern) - end - def match?(pattern, message) - command(pattern).match?(message) + matcher(pattern).match?(message) end class EqualTest < self -------------- next part -------------- HTML����������������������������... Download