[Groonga-commit] droonga/fluent-plugin-droonga at 372c84c [master] message matcher: prepare pattern in constructor

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Mar 23 19:57:28 JST 2014


Kouhei Sutou	2014-03-23 19:57:28 +0900 (Sun, 23 Mar 2014)

  New Revision: 372c84c18438aa98b65e4f0a5f376486e94cd587
  https://github.com/droonga/fluent-plugin-droonga/commit/372c84c18438aa98b65e4f0a5f376486e94cd587

  Message:
    message matcher: prepare pattern in constructor

  Modified files:
    lib/droonga/message_matcher.rb
    test/unit/test_message_matcher.rb

  Modified: lib/droonga/message_matcher.rb (+18 -15)
===================================================================
--- lib/droonga/message_matcher.rb    2014-03-23 18:22:00 +0900 (b789ab0)
+++ lib/droonga/message_matcher.rb    2014-03-23 19:57:28 +0900 (a6d01fa)
@@ -59,47 +59,50 @@ module Droonga
   class MessageMatcher
     # @param [Array] pattern The pattern to be matched against a message.
     def initialize(pattern)
-      @pattern = pattern
+      path, operator, *arguments = pattern
+      @path_components = path.split(".")
+      @operator = operator
+      @arguments = arguments
     end
 
     def match?(message)
-      return false if****@patte*****?
-      path, operator, *arguments = @pattern
-      target = resolve_path(path, message)
-      apply_operator(operator, target, arguments)
+      target = extract_target(message)
+      apply_operator(target)
     end
 
     private
     NONEXISTENT_PATH = Object.new
-    def resolve_path(path, message)
-      path.split(".").inject(message) do |result, component|
+    def extract_target(message)
+      result = message
+      @path_components.each do |component|
         return NONEXISTENT_PATH unless result.is_a?(Hash)
-        result[component]
+        result = result[component]
       end
+      result
     end
 
-    def apply_operator(operator, target, arguments)
-      case operator
+    def apply_operator(target)
+      case @operator
       when :equal
-        [target] == arguments
+        [target] == @arguments
       when :in
-        arguments.any? do |argument|
+        @arguments.any? do |argument|
           argument.include?(target)
         end
       when :include
         return false unless target.respond_to?(:include?)
-        arguments.any? do |argument|
+        @arguments.any? do |argument|
           target.include?(argument)
         end
       when :exist
         target != NONEXISTENT_PATH
       when :start_with
         return false unless target.respond_to?(:start_with?)
-        arguments.any? do |argument|
+        @arguments.any? do |argument|
           target.start_with?(argument)
         end
       else
-        raise ArgumentError, "Unknown operator: <#{operator}>"
+        raise ArgumentError, "Unknown operator: <#{@operator}>"
       end
     end
   end

  Modified: test/unit/test_message_matcher.rb (+15 -15)
===================================================================
--- test/unit/test_message_matcher.rb    2014-03-23 18:22:00 +0900 (fe66f38)
+++ test/unit/test_message_matcher.rb    2014-03-23 19:57:28 +0900 (48c36cf)
@@ -20,34 +20,34 @@ class MessageMatcherTest < Test::Unit::TestCase
     Droonga::MessageMatcher.new(pattern)
   end
 
-  class ResolvePathTest < self
-    def resolve_path(path, message)
-      matcher(nil).send(:resolve_path, path, message)
+  class ExtractTargetTest < self
+    def extract_target(path, message)
+      matcher([path]).send(:extract_target, message)
     end
 
     def test_nonexistent
       assert_equal(Droonga::MessageMatcher::NONEXISTENT_PATH,
-                   resolve_path("nonexistent.path", {}))
+                   extract_target("nonexistent.path", {}))
     end
 
     def test_top_level
       assert_equal("select",
-                   resolve_path("type",
-                                {
-                                  "type" => "select"
-                                }))
+                   extract_target("type",
+                                  {
+                                    "type" => "select"
+                                  }))
     end
 
     def test_nested
       assert_equal(10,
-                   resolve_path("body.output.limit",
-                                {
-                                  "body" => {
-                                    "output" => {
-                                      "limit" => 10,
+                   extract_target("body.output.limit",
+                                  {
+                                    "body" => {
+                                      "output" => {
+                                        "limit" => 10,
+                                      },
                                     },
-                                  },
-                                }))
+                                  }))
     end
   end
 
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index