[Groonga-commit] ranguba/groonga-client at 440f420 [master] Add geo_in_circle method in `select.rb`.

Back to archive index

HorimotoYasuhiro null+****@clear*****
Wed Apr 26 16:37:35 JST 2017


HorimotoYasuhiro	2017-04-26 16:37:35 +0900 (Wed, 26 Apr 2017)

  New Revision: 440f4202fc18fdab5f6cbd348b75dfba6032d6f9
  https://github.com/ranguba/groonga-client/commit/440f4202fc18fdab5f6cbd348b75dfba6032d6f9

  Merged dd932a0: Merge pull request #18 from komainu8/feature/add_geo_in_circle_method

  Message:
    Add geo_in_circle method in `select.rb`.

  Modified files:
    lib/groonga/client/request/select.rb
    test/request/test-select.rb

  Modified: lib/groonga/client/request/select.rb (+68 -0)
===================================================================
--- lib/groonga/client/request/select.rb    2017-04-26 15:19:43 +0900 (ed46d84)
+++ lib/groonga/client/request/select.rb    2017-04-26 16:37:35 +0900 (89af4de)
@@ -93,6 +93,11 @@ module Groonga
         #        filter.in_values("tags", "tag1", "tag2")
         #          # -> --filter 'in_values(tags, "tag1", "tag2")'
         #
+        #   @example Use geo_in_circle function
+        #      request.
+        #        filter.geo_in_circle("0x0", "100x100", 300)
+        #          # -> --filter 'geo_in_circle("0x0", "100x100", 300, "rectangle")'
+        #
         #   @example Use between function
         #      request.
         #        filter.between("age", 19, "include", 32, "include")
@@ -187,6 +192,44 @@ module Groonga
             @request = request
           end
 
+          # Adds a `geo_in_circle` condition then return a new `select`
+          # request object.
+          #
+          # @example Basic usage
+          #    request.
+          #      filter.geo_in_circle("0x0", "100x100", 300).
+          #        # -> --filter 'geo_in_circle("0x0", "100x100", 300, "rectangle")'
+          #
+          # @see http://groonga.org/docs/reference/functions/geo_in_circle.html
+          #   geo_in_circle function in the Groonga document
+          #
+          # @param point [String] Specify point for confirm whether to exit in circle or not.
+          #
+          # @param center [String] This value that center of circle.
+          #
+          # @param radious [Integer] This value that radious of circle.
+          #
+          # @param approximate_type ["rectangle", "sphere", "ellopsoid"]
+          #    This value that type of approximate of geographical.
+          #    If it is nil, approximate_type value is `"rectangle"`.
+          #    If it is `"rectangle"`, calcurate distance from radious
+          #    by approximate of rectangle.
+          #    If it is `"sphere"`, calcurate distance from radious
+          #    by approximate of sphere.
+          #    If it is `"ellopsoid"`, calcurate distance from radious
+          #    by approximate of ellopsoid.
+          #
+          # @return [Groonga::Client::Request::Select]
+          #   The new request with the given condition.
+          #
+          # @since 0.4.4
+          def geo_in_circle(point, center, radious_or_point, approximate_type="rectangle")
+            parameter = FilterGeoInCircleParameter.new(point,
+                                                       center, radious_or_point,
+                                                       approximate_type)
+            add_parameter(FilterMerger, parameter)
+          end
+
           # Adds a `between` condition then return a new `select`
           # request object.
           #
@@ -506,6 +549,30 @@ module Groonga
         end
 
         # @private
+        class FilterGeoInCircleParameter
+          include ScriptSyntaxValueEscapable
+
+          def initialize(point,
+                         center, radious,
+                         approximate_type)
+            @point = point
+            @center = center
+            @radious = radious
+            @approximate_type = approximate_type
+          end
+
+          def to_parameters
+            filter = "geo_in_circle(#{escape_script_syntax_value(@point)}"
+            filter << ", #{escape_script_syntax_value(@center)}"
+            filter << ", #{escape_script_syntax_value(@radious)}"
+            filter << ", #{escape_script_syntax_value(@approximate_type)}"
+            filter << ")"
+            {
+              filter: filter,
+            }
+          end
+        end
+
         class FilterBetweenParameter
           include ScriptSyntaxValueEscapable
 
@@ -532,6 +599,7 @@ module Groonga
           end
         end
 
+        # @private
         class FilterInValuesParameter
           include ScriptSyntaxValueEscapable
 

  Modified: test/request/test-select.rb (+18 -0)
===================================================================
--- test/request/test-select.rb    2017-04-26 15:19:43 +0900 (c6b9f40)
+++ test/request/test-select.rb    2017-04-26 16:37:35 +0900 (fc2ea7b)
@@ -92,6 +92,24 @@ class TestRequestSelect < Test::Unit::TestCase
                           }))
     end
 
+    sub_test_case("#geo_in_circle") do
+      def geo_in_circle(point,
+                        center, radious_or_point,
+                        approximate_type="rectangle")
+        @request.filter.geo_in_circle(point,
+                                      center, radious_or_point,
+                                      approximate_type).to_parameters
+      end
+
+      test("approximate type") do
+        assert_equal({
+                       :table => "posts",
+                       :filter => "geo_in_circle(\"100x100\", \"140x250\", 300, \"rectangle\")",
+                     },
+                     geo_in_circle("100x100", "140x250", 300, "rectangle"))
+      end
+    end
+
     sub_test_case("#between") do
       def between(column_name,
                   min, min_border,
-------------- next part --------------
HTML����������������������������...
Download 



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