Kouhei Sutou
null+****@clear*****
Wed Dec 7 10:59:59 JST 2016
Kouhei Sutou 2016-12-07 10:59:59 +0900 (Wed, 07 Dec 2016) New Revision: 8a2981f8ebf85e4f523b8214b869452a81ff8704 https://github.com/ranguba/groonga-client-model/commit/8a2981f8ebf85e4f523b8214b869452a81ff8704 Message: Support saving referenced records Added files: test/unit/test_load_value_generator.rb Modified files: lib/groonga-client-model.rb lib/groonga_client_model/record.rb Renamed files: lib/groonga_client_model/load_value_generator.rb (from test/unit/test-record.rb) Modified: lib/groonga-client-model.rb (+1 -0) =================================================================== --- lib/groonga-client-model.rb 2016-12-07 10:59:22 +0900 (fb1bfb6) +++ lib/groonga-client-model.rb 2016-12-07 10:59:59 +0900 (f1ea792) @@ -22,6 +22,7 @@ require "groonga_client_model/version" require "groonga_client_model/client" require "groonga_client_model/error" +require "groonga_client_model/load_value_generator" require "groonga_client_model/record" require "groonga_client_model/schema" Renamed: lib/groonga_client_model/load_value_generator.rb (+23 -19) 54% =================================================================== --- test/unit/test-record.rb 2016-12-07 10:59:22 +0900 (bc59450) +++ lib/groonga_client_model/load_value_generator.rb 2016-12-07 10:59:59 +0900 (65ab6d8) @@ -14,30 +14,34 @@ # 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 +require "date" - setup do - @record = LoadValuesRecord.new +module GroongaClientModel + class LoadValueGenerator + def initialize(record) + @record = record 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)) + def generate + load_value = {} + @record.attributes.each do |name, value| + load_value[name] = format_value(value) + end + load_value 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)) + private + def format_value(value) + case value + when Date + value.strftime("%Y-%m-%d 00:00:00") + when Time + value.strftime("%Y-%m-%d %H:%M:%S.%6N") + when Record + format_value(value._key) + else + value = value + end end end end Modified: lib/groonga_client_model/record.rb (+4 -21) =================================================================== --- lib/groonga_client_model/record.rb 2016-12-07 10:59:22 +0900 (0a893dd) +++ lib/groonga_client_model/record.rb 2016-12-07 10:59:59 +0900 (6b4f52d) @@ -14,8 +14,6 @@ # 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 @@ -190,9 +188,10 @@ module GroongaClientModel def upsert Client.open do |client| table = self.class.schema.tables[self.class.table_name] - values = load_values + load_value_generator = LoadValueGenerator.new(self) + value = load_value_generator.generate response = client.load(table: table.name, - values: [values], + values: [value], output_ids: "yes", command_version: "3") unless response.success? @@ -201,7 +200,7 @@ module GroongaClientModel raise RecordNotSaved.new(message, self) end if response.n_loaded_records.zero? - message = "Failed to save: #{values.inspect}" + message = "Failed to save: #{value.inspect}" raise RecordNotSaved.new(message, self) end if @new_record @@ -231,21 +230,5 @@ 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_load_value_generator.rb (+58 -0) 100644 =================================================================== --- /dev/null +++ test/unit/test_load_value_generator.rb 2016-12-07 10:59:59 +0900 (d213f94) @@ -0,0 +1,58 @@ +# 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 TestLoadValueGenerator < Test::Unit::TestCase + class Memo < GroongaClientModel::Record + class << self + def columns + GroongaClientModel::Schema::Columns.new("tag" => {}, + "created_at" => {}) + end + end + end + + class Tag < GroongaClientModel::Record + class << self + def columns + GroongaClientModel::Schema::Columns.new("_key" => {}) + end + end + end + + setup do + @memo = Memo.new + @generator = GroongaClientModel::LoadValueGenerator.new(@memo) + end + + test "Date" do + @memo.created_at = Date.new(2016, 12, 6) + assert_equal({"created_at" => "2016-12-06 00:00:00"}, + @generator.generate) + end + + test "Time" do + @memo.created_at = Time.local(2016, 12, 6, 18, 41, 24, 195422) + assert_equal({"created_at" => "2016-12-06 18:41:24.195422"}, + @generator.generate) + end + + test "GroongaClientModel::Record" do + tag = Tag.new(_key: "important") + @memo.tag = tag + assert_equal({"tag" => "important"}, + @generator.generate) + end +end -------------- next part -------------- HTML����������������������������... Descargar