[Groonga-commit] ranguba/groonga-client-model at 3ddafa9 [master] Support "XXX?" for Bool columns

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Mar 9 13:42:49 JST 2017


Kouhei Sutou	2017-03-09 13:42:49 +0900 (Thu, 09 Mar 2017)

  New Revision: 3ddafa90516f7a8d14189b578a66e037391ea367
  https://github.com/ranguba/groonga-client-model/commit/3ddafa90516f7a8d14189b578a66e037391ea367

  Message:
    Support "XXX?" for Bool columns

  Modified files:
    lib/groonga_client_model/record.rb
    test/unit/record/test_active_model.rb
    test/unit/record/test_readers.rb
    test/unit/record/test_validators.rb
    test/unit/test_helper.rb
    test/unit/test_load_value_generator.rb

  Modified: lib/groonga_client_model/record.rb (+20 -1)
===================================================================
--- lib/groonga_client_model/record.rb    2017-03-08 11:29:37 +0900 (b3ac6be)
+++ lib/groonga_client_model/record.rb    2017-03-09 13:42:49 +0900 (149a751)
@@ -52,8 +52,21 @@ module GroongaClientModel
       def define_attributes
         return if defined?(@defined)
         @defined = true
+
+        boolean_column_names = []
+        non_boolean_column_names = []
+        columns.each do |name, column|
+          if (column["value_type"] || {})["name"] == "Bool"
+            boolean_column_names << name
+          else
+            non_boolean_column_names << name
+          end
+        end
+
         attribute_method_suffix("=")
-        define_attribute_methods(*columns.names)
+        define_attribute_methods(*non_boolean_column_names)
+        attribute_method_suffix("?")
+        define_attribute_methods(*boolean_column_names)
       end
 
       def count
@@ -135,6 +148,12 @@ module GroongaClientModel
           @attributes[name] = value
         end
       end
+
+      def define_method_attribute?(name)
+        define_method("#{name}?") do
+          @attributes[name]
+        end
+      end
     end
 
     define_model_callbacks :save, :create, :update, :destroy

  Modified: test/unit/record/test_active_model.rb (+1 -6)
===================================================================
--- test/unit/record/test_active_model.rb    2017-03-08 11:29:37 +0900 (a6ce9dd)
+++ test/unit/record/test_active_model.rb    2017-03-09 13:42:49 +0900 (0f7d709)
@@ -15,15 +15,10 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 class TestRecordActiveModel < Test::Unit::TestCase
-  Column = Groonga::Client::Response::Schema::Column
-
   class EmptyModel < GroongaClientModel::Record
     class << self
       def columns
-        raw_columns = {
-          "_id" => Column.new(nil, {}),
-        }
-        GroongaClientModel::Schema::Columns.new(nil, raw_columns)
+        TestHelper::Columns.build
       end
     end
   end

  Modified: test/unit/record/test_readers.rb (+12 -3)
===================================================================
--- test/unit/record/test_readers.rb    2017-03-08 11:29:37 +0900 (7d283b0)
+++ test/unit/record/test_readers.rb    2017-03-09 13:42:49 +0900 (93553c9)
@@ -15,12 +15,14 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 class TestRecordReaders < Test::Unit::TestCase
-  Column = Groonga::Client::Response::Schema::Column
-
   class Memo < GroongaClientModel::Record
     class << self
       def columns
-        GroongaClientModel::Schema::Columns.new(nil, "_id" => {})
+        TestHelper::Columns.build("published" => {
+                                    "value_type" => {
+                                      "name" => "Bool",
+                                    },
+                                  })
       end
     end
   end
@@ -33,4 +35,11 @@ class TestRecordReaders < Test::Unit::TestCase
     @memo._id = 29
     assert_equal(29, @memo.id)
   end
+
+  test "predicate" do
+    @memo.published = true
+    assert do
+      @memo.published?
+    end
+  end
 end

  Modified: test/unit/record/test_validators.rb (+6 -16)
===================================================================
--- test/unit/record/test_validators.rb    2017-03-08 11:29:37 +0900 (718b7bc)
+++ test/unit/record/test_validators.rb    2017-03-09 13:42:49 +0900 (b244c8a)
@@ -15,16 +15,11 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 class TestRecordValidators < Test::Unit::TestCase
-  Column = Groonga::Client::Response::Schema::Column
-
   sub_test_case("_key") do
     class NoKey < GroongaClientModel::Record
       class << self
         def columns
-          raw_columns = {
-            "_id" => Column.new(nil, {}),
-          }
-          GroongaClientModel::Schema::Columns.new(nil, raw_columns)
+          TestHelper::Columns.build
         end
       end
     end
@@ -32,16 +27,11 @@ class TestRecordValidators < Test::Unit::TestCase
     class Key < GroongaClientModel::Record
       class << self
         def columns
-          raw_columns = {
-            "_id" => Column.new(nil, {}),
-            "_key" => Column.new(nil, {
-                                   "name" => "_key",
-                                   "value_type" => {
-                                     "name" => key_type,
-                                   },
-                                 }),
-          }
-          GroongaClientModel::Schema::Columns.new(nil, raw_columns)
+          TestHelper::Columns.build("_key" => {
+                                      "value_type" => {
+                                        "name" => key_type,
+                                      },
+                                    })
         end
       end
     end

  Modified: test/unit/test_helper.rb (+25 -0)
===================================================================
--- test/unit/test_helper.rb    2017-03-08 11:29:37 +0900 (ca435fa)
+++ test/unit/test_helper.rb    2017-03-09 13:42:49 +0900 (0ffe827)
@@ -25,6 +25,31 @@ require "groonga_client_model/migration"
 GroongaClientModel::Client.url = "http://127.0.0.1:20041"
 
 module TestHelper
+  module Columns
+    Column = Groonga::Client::Response::Schema::Column
+
+    class << self
+      def build(definitions={})
+        raw_schema = nil
+        raw_columns = {}
+        default_definition = {
+          "indexes" => [],
+          "value_type" => nil,
+        }
+        id_definition = default_definition.merge("name" => "_id",
+                                                 "value_type" => {
+                                                   "name" => "UInt32",
+                                                 })
+        raw_columns["_id"] = Column.new(raw_schema, id_definition)
+        definitions.each do |name, definition|
+          definition = default_definition.merge("name" => name).merge(definition)
+          raw_columns[name] = Column.new(raw_schema, definition)
+        end
+        GroongaClientModel::Schema::Columns.new(raw_schema, raw_columns)
+      end
+    end
+  end
+
   module Migration
     def open_client(&block)
       GroongaClientModel::Client.open(&block)

  Modified: test/unit/test_load_value_generator.rb (+8 -7)
===================================================================
--- test/unit/test_load_value_generator.rb    2017-03-08 11:29:37 +0900 (66e883e)
+++ test/unit/test_load_value_generator.rb    2017-03-09 13:42:49 +0900 (01d66ae)
@@ -18,10 +18,9 @@ class TestLoadValueGenerator < Test::Unit::TestCase
   class Memo < GroongaClientModel::Record
     class << self
       def columns
-        GroongaClientModel::Schema::Columns.new(nil,
-                                                "tag" => {},
-                                                "tags" => {},
-                                                "created_at" => {})
+        TestHelper::Columns.build("tag" => {},
+                                  "tags" => {},
+                                  "created_at" => {})
       end
     end
   end
@@ -29,9 +28,11 @@ class TestLoadValueGenerator < Test::Unit::TestCase
   class Tag < GroongaClientModel::Record
     class << self
       def columns
-        GroongaClientModel::Schema::Columns.new(nil,
-                                                "_id" => {},
-                                                "_key" => {})
+        TestHelper::Columns.build("_key" => {
+                                    "value_type" => {
+                                      "name" => "ShortText",
+                                    },
+                                  })
       end
     end
   end
-------------- next part --------------
HTML����������������������������...
Download 



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