[Groonga-commit] ranguba/groonga-client-model at 8a48743 [master] Use Active Model like naming rule

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Feb 6 13:06:41 JST 2017


Kouhei Sutou	2017-02-06 13:06:41 +0900 (Mon, 06 Feb 2017)

  New Revision: 8a487435aee1d08a557befda22210a1517cde696
  https://github.com/ranguba/groonga-client-model/commit/8a487435aee1d08a557befda22210a1517cde696

  Message:
    Use Active Model like naming rule

  Modified files:
    lib/groonga_client_model/locale/en.yml
    lib/groonga_client_model/validations/type_validator.rb
    test/unit/test_record.rb

  Modified: lib/groonga_client_model/locale/en.yml (+10 -12)
===================================================================
--- lib/groonga_client_model/locale/en.yml    2017-02-06 12:57:17 +0900 (91cf410)
+++ lib/groonga_client_model/locale/en.yml    2017-02-06 13:06:41 +0900 (aab86ca)
@@ -1,23 +1,21 @@
 en:
   errors:
     messages:
-      uint:
-        "must be positive integer: %{inspected_value}"
-      uint8:
+      not_a_positive_integer:
+        "must be a positive integer: %{inspected_value}"
+      invalid_uint8:
         "must be less than 2 ** 8: %{inspected_value}"
-      uint16:
+      invalid_uint16:
         "must be less than 2 ** 16: %{inspected_value}"
-      uint32:
+      invalid_uint32:
         "must be less than 2 ** 32: %{inspected_value}"
-      uint64:
+      invalid_uint64:
         "must be less than 2 ** 64: %{inspected_value}"
-      int:
-        "must be integer: %{inspected_value}"
-      int8:
+      invalid_int8:
         "must be between -(2 ** 7) and (2 ** 7) - 1: %{inspected_value}"
-      int16:
+      invalid_int16:
         "must be between -(2 ** 15) and (2 ** 15) - 1: %{inspected_value}"
-      int32:
+      invalid_int32:
         "must be between -(2 ** 31) and (2 ** 31) - 1: %{inspected_value}"
-      int64:
+      invalid_int64:
         "must be between -(2 ** 63) and (2 ** 63) - 1: %{inspected_value}"

  Modified: lib/groonga_client_model/validations/type_validator.rb (+22 -7)
===================================================================
--- lib/groonga_client_model/validations/type_validator.rb    2017-02-06 12:57:17 +0900 (01c922f)
+++ lib/groonga_client_model/validations/type_validator.rb    2017-02-06 13:06:41 +0900 (ecbefbd)
@@ -40,6 +40,8 @@ module GroongaClientModel
           validate_int(record, attribute, value, 32)
         when "Int64"
           validate_int(record, attribute, value, 64)
+        when "Float"
+          validate_float(record, attribute, value)
         end
       end
 
@@ -56,19 +58,19 @@ module GroongaClientModel
         when Numeric
           if value < 0
             record.errors.add(attribute,
-                              :uint,
+                              :not_a_positive_integer,
                               options.merge(inspected_value: value.inspect))
             return
           end
           if value > ((2 ** n_bits) - 1)
             record.errors.add(attribute,
-                              :"uint#{n_bits}",
+                              :"invalid_uint#{n_bits}",
                               options.merge(inspected_value: value.inspect))
             return
           end
         else
           record.errors.add(attribute,
-                            :uint,
+                            :not_a_positive_integer,
                             options.merge(inspected_value: value.inspect))
         end
       end
@@ -87,14 +89,27 @@ module GroongaClientModel
           max = ((2 ** (n_bits - 1)) - 1)
           if value < min or value > max
             record.errors.add(attribute,
-                              :"int#{n_bits}",
+                              :"invalid_int#{n_bits}",
                               options.merge(inspected_value: value.inspect))
             return
           end
         else
-          record.errors.add(attribute,
-                            :int,
-                            options.merge(inspected_value: value.inspect))
+          record.errors.add(attribute, :not_an_integer)
+        end
+      end
+
+      def validate_float(record, attribute, value)
+        if value.is_a?(String)
+          begin
+            value = Float(value)
+          rescue ArgumentError
+          end
+        end
+
+        case value
+        when Numeric
+        else
+          record.errors.add(attribute, :not_a_number)
         end
       end
     end

  Modified: test/unit/test_record.rb (+34 -20)
===================================================================
--- test/unit/test_record.rb    2017-02-06 12:57:17 +0900 (0349ffc)
+++ test/unit/test_record.rb    2017-02-06 13:06:41 +0900 (1f95b3b)
@@ -157,6 +157,14 @@ class TestRecord < Test::Unit::TestCase
         end
       end
 
+      class FloatKey < Key
+        class << self
+          def key_type
+            "Float"
+          end
+        end
+      end
+
       sub_test_case("presence") do
         test "no key" do
           record = NoKey.new
@@ -218,97 +226,103 @@ class TestRecord < Test::Unit::TestCase
 
         sub_test_case("UInt8") do
           test("invalid") do
-            assert_invalid(UInt8Key, "String", :uint)
+            assert_invalid(UInt8Key, "String", :not_a_positive_integer)
           end
 
           test("too large") do
-            assert_invalid(UInt8Key, 2 ** 8, :uint8)
+            assert_invalid(UInt8Key, 2 ** 8, :invalid_uint8)
           end
         end
 
         sub_test_case("UInt16") do
           test("invalid") do
-            assert_invalid(UInt16Key, "String", :uint)
+            assert_invalid(UInt16Key, "String", :not_a_positive_integer)
           end
 
           test("too large") do
-            assert_invalid(UInt16Key, 2 ** 16, :uint16)
+            assert_invalid(UInt16Key, 2 ** 16, :invalid_uint16)
           end
         end
 
         sub_test_case("UInt32") do
           test("invalid") do
-            assert_invalid(UInt32Key, "String", :uint)
+            assert_invalid(UInt32Key, "String", :not_a_positive_integer)
           end
 
           test("too large") do
-            assert_invalid(UInt32Key, 2 ** 32, :uint32)
+            assert_invalid(UInt32Key, 2 ** 32, :invalid_uint32)
           end
         end
 
         sub_test_case("UInt64") do
           test("invalid") do
-            assert_invalid(UInt64Key, "String", :uint)
+            assert_invalid(UInt64Key, "String", :not_a_positive_integer)
           end
 
           test("too large") do
-            assert_invalid(UInt64Key, 2 ** 64, :uint64)
+            assert_invalid(UInt64Key, 2 ** 64, :invalid_uint64)
           end
         end
 
         sub_test_case("Int8") do
           test("invalid") do
-            assert_invalid(Int8Key, "String", :int)
+            assert_invalid(Int8Key, "String", :not_an_integer)
           end
 
           test("too small") do
-            assert_invalid(Int8Key, -(2 ** 7) - 1, :int8)
+            assert_invalid(Int8Key, -(2 ** 7) - 1, :invalid_int8)
           end
 
           test("too large") do
-            assert_invalid(Int8Key, 2 ** 7, :int8)
+            assert_invalid(Int8Key, 2 ** 7, :invalid_int8)
           end
         end
 
         sub_test_case("Int16") do
           test("invalid") do
-            assert_invalid(Int16Key, "String", :int)
+            assert_invalid(Int16Key, "String", :not_an_integer)
           end
 
           test("too small") do
-            assert_invalid(Int16Key, -(2 ** 15) - 1, :int16)
+            assert_invalid(Int16Key, -(2 ** 15) - 1, :invalid_int16)
           end
 
           test("too large") do
-            assert_invalid(Int16Key, 2 ** 15, :int16)
+            assert_invalid(Int16Key, 2 ** 15, :invalid_int16)
           end
         end
 
         sub_test_case("Int32") do
           test("invalid") do
-            assert_invalid(Int32Key, "String", :int)
+            assert_invalid(Int32Key, "String", :not_an_integer)
           end
 
           test("too small") do
-            assert_invalid(Int32Key, -(2 ** 31) - 1, :int32)
+            assert_invalid(Int32Key, -(2 ** 31) - 1, :invalid_int32)
           end
 
           test("too large") do
-            assert_invalid(Int32Key, 2 ** 31, :int32)
+            assert_invalid(Int32Key, 2 ** 31, :invalid_int32)
           end
         end
 
         sub_test_case("Int64") do
           test("invalid") do
-            assert_invalid(Int64Key, "String", :int)
+            assert_invalid(Int64Key, "String", :not_an_integer)
           end
 
           test("too small") do
-            assert_invalid(Int64Key, -(2 ** 63) - 1, :int64)
+            assert_invalid(Int64Key, -(2 ** 63) - 1, :invalid_int64)
           end
 
           test("too large") do
-            assert_invalid(Int64Key, 2 ** 63, :int64)
+            assert_invalid(Int64Key, 2 ** 63, :invalid_int64)
+          end
+        end
+
+        sub_test_case("Float") do
+          test("invalid") do
+            assert_invalid(FloatKey, "String", :not_a_number)
           end
         end
       end
-------------- next part --------------
HTML����������������������������...
Descargar 



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