Kouhei Sutou
null+****@clear*****
Fri Feb 3 18:28:45 JST 2017
Kouhei Sutou 2017-02-03 18:28:45 +0900 (Fri, 03 Feb 2017) New Revision: bde8c3a6cc0726dc5577e7283100e18bf9f641f7 https://github.com/ranguba/groonga-client-model/commit/bde8c3a6cc0726dc5577e7283100e18bf9f641f7 Message: Add _key type validation by default Added files: lib/groonga_client_model/validations/type_validator.rb test/apps/rails5/app/models/age.rb test/apps/rails5/test/factories/ages.rb test/apps/rails5/test/models/age_test.rb Modified files: lib/groonga-client-model.rb lib/groonga_client_model/record.rb lib/groonga_client_model/schema.rb test/apps/rails5/Gemfile.lock test/apps/rails5/db/schema.grn Modified: lib/groonga-client-model.rb (+4 -2) =================================================================== --- lib/groonga-client-model.rb 2017-01-27 13:35:08 +0900 (334d664) +++ lib/groonga-client-model.rb 2017-02-03 18:28:45 +0900 (4e88b73) @@ -1,4 +1,4 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2016-2017 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 @@ -27,9 +27,11 @@ require "groonga_client_model/error" require "groonga_client_model/load_value_generator" require "groonga_client_model/modelizable" require "groonga_client_model/modelize" -require "groonga_client_model/record" require "groonga_client_model/schema" require "groonga_client_model/schema_loader" +require "groonga_client_model/validations/type_validator" + +require "groonga_client_model/record" module GroongaClientModel extend ActiveSupport::Autoload Modified: lib/groonga_client_model/record.rb (+5 -2) =================================================================== --- lib/groonga_client_model/record.rb 2017-01-27 13:35:08 +0900 (cd558eb) +++ lib/groonga_client_model/record.rb 2017-02-03 18:28:45 +0900 (9e8f8f1) @@ -1,4 +1,4 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2016-2017 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 @@ -138,7 +138,10 @@ module GroongaClientModel attr_reader :attributes - validates :_key, presence: true, if: ->(record) {record.class.have_key?} + validates :_key, + presence: true, + "groonga_client_model/validations/type": true, + if: ->(record) {record.class.have_key?} def initialize(attributes=nil) @attributes = {} Modified: lib/groonga_client_model/schema.rb (+14 -7) =================================================================== --- lib/groonga_client_model/schema.rb 2017-01-27 13:35:08 +0900 (8370015) +++ lib/groonga_client_model/schema.rb 2017-02-03 18:28:45 +0900 (0c633fe) @@ -1,4 +1,4 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2016-2017 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 @@ -62,18 +62,20 @@ module GroongaClientModel def columns raw_columns = {} - raw_columns["_id"] = create_pseudo_column("_id") - if @raw_table.key_type - raw_columns["_key"] = create_pseudo_column("_key") + raw_columns["_id"] = create_pseudo_column("_id", {"name" => "UInt32"}) + key_type = @raw_table.key_type + if key_type + raw_columns["_key"] = create_pseudo_column("_key", key_type) end Columns.new(@raw_schema, @raw_table.columns.merge(raw_columns)) end private - def create_pseudo_column(name) + def create_pseudo_column(name, value_type) raw_column = { "name" => name, "indexes" => [], + "value_type" => value_type, } Groonga::Client::Response::Schema::Column.new(@raw_schema, raw_column) end @@ -88,7 +90,7 @@ module GroongaClientModel end def exist?(name) - @raw_columns.key?(name) + @raw_columns.key?(normalize_name(name)) end def names @@ -96,7 +98,7 @@ module GroongaClientModel end def [](name) - @raw_columns[name] + @raw_columns[normalize_name(name)] end def each @@ -104,6 +106,11 @@ module GroongaClientModel yield(name, column) end end + + private + def normalize_name(name) + name.to_s + end end end end Added: lib/groonga_client_model/validations/type_validator.rb (+55 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga_client_model/validations/type_validator.rb 2017-02-03 18:28:45 +0900 (9120972) @@ -0,0 +1,55 @@ +# Copyright (C) 2017 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 + +module GroongaClientModel + module Validations + class TypeValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + column = record.class.columns[attribute] + return if column.nil? + value_type = column.value_type + return if value_type.nil? + + case value_type.name + when "UInt32" + validate_uint32(record, attribute, value) + end + end + + private + def validate_uint32(record, attribute, value) + if value.is_a?(String) + begin + value = Integer(value) + rescue ArgumentError + end + end + + case value + when Numeric + return if value >= 0 + record.errors.add(attribute, + "must be positive integer: #{value.inspect}", + options) + else + record.errors.add(attribute, + "must be positive integer: #{value.inspect}", + options) + end + end + end + end +end Modified: test/apps/rails5/Gemfile.lock (+1 -1) =================================================================== --- test/apps/rails5/Gemfile.lock 2017-01-27 13:35:08 +0900 (c8fe96a) +++ test/apps/rails5/Gemfile.lock 2017-02-03 18:28:45 +0900 (8c77876) @@ -10,7 +10,7 @@ PATH PATH remote: ../../../ specs: - groonga-client-model (0.9.8) + groonga-client-model (0.9.9) activemodel groonga-client (>= 0.3.7) groonga-command-parser Added: test/apps/rails5/app/models/age.rb (+2 -0) 100644 =================================================================== --- /dev/null +++ test/apps/rails5/app/models/age.rb 2017-02-03 18:28:45 +0900 (bd39c32) @@ -0,0 +1,2 @@ +class Age < ApplicationGroongaRecord +end Modified: test/apps/rails5/db/schema.grn (+2 -0) =================================================================== --- test/apps/rails5/db/schema.grn 2017-01-27 13:35:08 +0900 (aaf10e5) +++ test/apps/rails5/db/schema.grn 2017-02-03 18:28:45 +0900 (73c86b5) @@ -7,3 +7,5 @@ table_create terms TABLE_PAT_KEY ShortText \ --default_tokenizer TokenBigram \ --normalizer NormalizerAuto column_create terms posts_body COLUMN_INDEX|WITH_POSITION posts body + +table_create ages TABLE_HASH_KEY UInt32 Added: test/apps/rails5/test/factories/ages.rb (+5 -0) 100644 =================================================================== --- /dev/null +++ test/apps/rails5/test/factories/ages.rb 2017-02-03 18:28:45 +0900 (2becd6c) @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :age do + _key 29 + end +end Added: test/apps/rails5/test/models/age_test.rb (+10 -0) 100644 =================================================================== --- /dev/null +++ test/apps/rails5/test/models/age_test.rb 2017-02-03 18:28:45 +0900 (5058633) @@ -0,0 +1,10 @@ +require 'test_helper' + +class AgeTest < ActiveSupport::TestCase + test "validate: _key: invalid: string" do + age = Age.new(_key: "Hello") + assert(age.invalid?) + assert_equal(["Key must be positive integer: \"Hello\""], + age.errors.full_messages) + end +end -------------- next part -------------- HTML����������������������������...Download