[Groonga-commit] droonga/fluent-plugin-droonga at 1c2d5e7 [master] Accept multiple expected types for parameter validation

Back to archive index

YUKI Hiroshi null+****@clear*****
Thu Feb 13 10:55:24 JST 2014


YUKI Hiroshi	2014-02-13 10:55:24 +0900 (Thu, 13 Feb 2014)

  New Revision: 1c2d5e7bc4fa608fbe2e03dec792687395c224d4
  https://github.com/droonga/fluent-plugin-droonga/commit/1c2d5e7bc4fa608fbe2e03dec792687395c224d4

  Message:
    Accept multiple expected types for parameter validation

  Modified files:
    lib/droonga/catalog/base.rb
    lib/droonga/catalog/errors.rb

  Modified: lib/droonga/catalog/base.rb (+12 -6)
===================================================================
--- lib/droonga/catalog/base.rb    2014-02-13 10:45:41 +0900 (5d3f302)
+++ lib/droonga/catalog/base.rb    2014-02-13 10:55:24 +0900 (d21542b)
@@ -178,13 +178,19 @@ module Droonga
         raise MissingRequiredParameter.new(name, @path) unless value
       end
 
-      def validate_parameter_type(expected, value, name)
-        unless value.is_a?(expected)
-          raise MismatchedParameterType.new(name,
-                                            expected,
-                                            value.class,
-                                            @path)
+      def validate_parameter_type(expected_types, value, name)
+        expected_types = [expected_types] unless expected_types.is_a?(Array)
+
+        if expected_types.any? do |type|
+             value.is_a?(type)
+           end
+          return
         end
+
+        raise MismatchedParameterType.new(name,
+                                          expected,
+                                          value.class,
+                                          @path)
       end
 
       def validate_valid_datetime(value, name)

  Modified: lib/droonga/catalog/errors.rb (+11 -2)
===================================================================
--- lib/droonga/catalog/errors.rb    2014-02-13 10:45:41 +0900 (980555a)
+++ lib/droonga/catalog/errors.rb    2014-02-13 10:55:24 +0900 (f83fa21)
@@ -34,8 +34,17 @@ module Droonga
     end
 
     class MismatchedParameterType < ValidationError
-      def initialize(name, expected, actual, path)
-        super("\"#{name}\" must be a #{expected}, but a #{actual}.", path)
+      def initialize(name, expected_types, actual, path)
+        expected_types = [expected_types] unless expected_types.is_a?(Array)
+        message = nil
+        if expected_types.size == 1
+          message = "\"#{name}\" must be #{expected_types.first}, " +
+                      "but #{actual}."
+        else
+          message = "\"#{name}\" must be #{expected_types.join(" or ")}, " +
+                      "but #{actual}."
+        end
+        super(message, path)
       end
     end
 
-------------- next part --------------
HTML����������������������������...
Download 



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