[Groonga-commit] ranguba/groonga-client-model at 162ec64 [master] Support loading Date and Time

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Dec 6 18:47:00 JST 2016


Kouhei Sutou	2016-12-06 18:47:00 +0900 (Tue, 06 Dec 2016)

  New Revision: 162ec64b10b4b95882ca891736fabe68b229dfa0
  https://github.com/ranguba/groonga-client-model/commit/162ec64b10b4b95882ca891736fabe68b229dfa0

  Message:
    Support loading Date and Time

  Added files:
    test/unit/test-record.rb
  Modified files:
    .travis.yml
    lib/groonga_client_model/record.rb

  Modified: .travis.yml (+1 -1)
===================================================================
--- .travis.yml    2016-12-06 18:24:03 +0900 (bb7a26a)
+++ .travis.yml    2016-12-06 18:47:00 +0900 (5c78afc)
@@ -18,7 +18,7 @@ install:
         BUNDLE_GEMFILE=${PWD}/Gemfile bundle install --jobs=3 --retry=3)
     done
 script:
-#  - bundle exec ruby test/unit/run-test.rb
+  - bundle exec ruby test/unit/run-test.rb
   - |
     for test_app in ${PWD}/test/apps/*
     do

  Modified: lib/groonga_client_model/record.rb (+19 -3)
===================================================================
--- lib/groonga_client_model/record.rb    2016-12-06 18:24:03 +0900 (b6d3031)
+++ lib/groonga_client_model/record.rb    2016-12-06 18:47:00 +0900 (f6b8eb4)
@@ -14,6 +14,8 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
+require "date"
+
 module GroongaClientModel
   class Record
     extend ActiveModel::Naming
@@ -38,8 +40,6 @@ module GroongaClientModel
         return if defined?(@defined)
         @defined = true
         columns.each do |name, column|
-          define_attribute_methods name
-
           attribute_method_suffix "="
           define_attribute_methods name
         end
@@ -193,7 +193,7 @@ module GroongaClientModel
       Client.open do |client|
         table = self.class.schema.tables[self.class.table_name]
         response = client.load(table: table.name,
-                               values: [attributes],
+                               values: [load_values],
                                output_ids: "yes",
                                command_version: "3")
         unless response.success?
@@ -232,5 +232,21 @@ module GroongaClientModel
         true
       end
     end
+
+    def load_values
+      values = {}
+      @attributes.each do |name, value|
+        case value
+        when Date
+          value = value.strftime("%Y-%m-%d 00:00:00")
+        when Time
+          value = value.strftime("%Y-%m-%d %H:%M:%S.%6N")
+        else
+          value = value
+        end
+        values[name] = value
+      end
+      values
+    end
   end
 end

  Added: test/unit/test-record.rb (+43 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/test-record.rb    2016-12-06 18:47:00 +0900 (bc59450)
@@ -0,0 +1,43 @@
+# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+class RecordTest < Test::Unit::TestCase
+  sub_test_case("#load_values") do
+    class LoadValuesRecord < GroongaClientModel::Record
+      class << self
+        def columns
+          GroongaClientModel::Schema::Columns.new({"created_at" => {}})
+        end
+      end
+    end
+
+    setup do
+      @record = LoadValuesRecord.new
+    end
+
+    def test_date
+      @record.created_at = Date.new(2016, 12, 6)
+      assert_equal({"created_at" => "2016-12-06 00:00:00"},
+                   @record.__send__(:load_values))
+    end
+
+    def test_time
+      @record.created_at = Time.local(2016, 12, 6, 18, 41, 24, 195422)
+      assert_equal({"created_at" => "2016-12-06 18:41:24.195422"},
+                   @record.__send__(:load_values))
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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