[Groonga-commit] ranguba/groonga-client-rails at 234a092 [master] Support custom column value

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Mar 23 14:51:40 JST 2016


Kouhei Sutou	2016-03-23 14:51:40 +0900 (Wed, 23 Mar 2016)

  New Revision: 234a092a47ad1a1e8dc394f61acbac320f3e7968
  https://github.com/ranguba/groonga-client-rails/commit/234a092a47ad1a1e8dc394f61acbac320f3e7968

  Message:
    Support custom column value

  Added files:
    test/fixtures/rails4-mongoid/test/searchers/posts_searcher_test.rb
  Copied files:
    lib/groonga/client/searcher/source.rb
      (from lib/groonga/client/searcher/schema.rb)
  Removed files:
    lib/groonga/client/searcher/source_definition.rb
  Modified files:
    lib/groonga/client/searcher.rb
    lib/groonga/client/searcher/schema.rb
    test/fixtures/rails4-mongoid/app/models/post.rb

  Modified: lib/groonga/client/searcher.rb (+48 -36)
===================================================================
--- lib/groonga/client/searcher.rb    2016-03-23 14:44:43 +0900 (cc19981)
+++ lib/groonga/client/searcher.rb    2016-03-23 14:51:40 +0900 (7e090a2)
@@ -18,7 +18,7 @@ require "groonga/client/searcher/request"
 require "groonga/client/searcher/result_set"
 require "groonga/client/searcher/schema"
 require "groonga/client/searcher/schema_synchronizer"
-require "groonga/client/searcher/source_definition"
+require "groonga/client/searcher/source"
 
 module Groonga
   class Client
@@ -28,28 +28,8 @@ module Groonga
           @schema ||= Schema.new(default_table_name)
         end
 
-        def add_source(model_class, columns:)
-          sources[model_class] = SourceDefinition.new(model_class, columns)
-
-          searcher_class = self
-          model_class.after_create do |model|
-            searcher = searcher_class.new
-            searcher.create(model)
-          end
-
-          model_class.after_update do |model|
-            searcher = searcher_class.new
-            searcher.update(model)
-          end
-
-          model_class.after_destroy do |model|
-            searcher = searcher_class.new
-            searcher.destroy(model)
-          end
-        end
-
-        def fetch_source_definition(source)
-          sources.fetch(source.class)
+        def source(model_class)
+          sources[model_class] ||= create_source(model_class)
         end
 
         def sync
@@ -86,6 +66,28 @@ module Groonga
           @sources ||= {}
         end
 
+        def create_source(model_class)
+          source = Source.new(schema, model_class)
+
+          searcher_class = self
+          model_class.after_create do |model|
+            searcher = searcher_class.new
+            searcher.create(model)
+          end
+
+          model_class.after_update do |model|
+            searcher = searcher_class.new
+            searcher.update(model)
+          end
+
+          model_class.after_destroy do |model|
+            searcher = searcher_class.new
+            searcher.destroy(model)
+          end
+
+          source
+        end
+
         def default_table_name
           name.gsub(/Searcher\z/, "").tableize
         end
@@ -98,31 +100,41 @@ module Groonga
       def inititalize
       end
 
-      def upsert(source)
-        definition = self.class.fetch_source_definition(source)
+      def upsert(model)
+        source = self.class.source(model.class)
         record = {}
-        definition.columns.each do |name, _|
-          record[name.to_s] = source.__send__(name)
+        source.columns.each do |name, reader|
+          case reader
+          when Symbol
+            value = model.__send__(reader)
+          when TrueClass
+            value = model.__send__(name)
+          when NilClass
+            next
+          else
+            value = reader.call(model)
+          end
+          record[name] = value
         end
-        record["_key"] = source_key(source)
+        record["_key"] = model_key(model)
         Client.open do |client|
           client.load(:table => self.class.schema.table,
                       :values => [record])
         end
       end
 
-      def create(source)
-        upsert(source)
+      def create(model)
+        upsert(model)
       end
 
-      def update(source)
-        upsert(source)
+      def update(model)
+        upsert(model)
       end
 
-      def destroy(source)
+      def destroy(model)
         Client.open do |client|
           client.delete(table: self.class.schema.table,
-                        key: source_key(source))
+                        key: model_key(model))
         end
       end
 
@@ -131,8 +143,8 @@ module Groonga
       end
 
       private
-      def source_key(source)
-        "#{source.class.name}-#{source.id}"
+      def model_key(model)
+        "#{model.class.name}-#{model.id}"
       end
     end
   end

  Modified: lib/groonga/client/searcher/schema.rb (+5 -0)
===================================================================
--- lib/groonga/client/searcher/schema.rb    2016-03-23 14:44:43 +0900 (90ccbd9)
+++ lib/groonga/client/searcher/schema.rb    2016-03-23 14:51:40 +0900 (a2546ee)
@@ -35,6 +35,11 @@ module Groonga
           @columns[name] = Column.new(name, options)
         end
 
+        def have_column?(name)
+          name = normalize_name(name)
+          @columns.key?(name)
+        end
+
         private
         def normalize_name(name)
           if name.is_a?(Symbol)

  Copied: lib/groonga/client/searcher/source.rb (+23 -33) 51%
===================================================================
--- lib/groonga/client/searcher/schema.rb    2016-03-23 14:44:43 +0900 (90ccbd9)
+++ lib/groonga/client/searcher/source.rb    2016-03-23 14:51:40 +0900 (d579178)
@@ -17,51 +17,41 @@
 module Groonga
   class Client
     class Searcher
-      class Schema
-        attr_reader :table
+      class Source
+        attr_reader :model_class
         attr_reader :columns
-        def initialize(table)
-          @table = table
+        def initialize(schema, model_class)
+          @schema = schema
+          @model_class = model_class
           @columns = {}
         end
 
-        def table=(name)
-          name = name.to_s if name.is_a?(Symbol)
-          @table = name
+        def []=(name, reader)
+          unles****@schem*****_column?(name)
+            message = "unknown column name: #{name.inspect}"
+            available_columns =****@schem*****(", ")
+            message << "available columns: [#{available_columns}]"
+            raise ArgumentError, message
+          end
+          @columns[name.to_s] = reader
         end
 
-        def column(name, options)
-          name = normalize_name(name)
-          @columns[name] = Column.new(name, options)
-        end
+        def method_missing(name, *args, &block)
+          return super unless name.to_s.end_with?("=")
 
-        private
-        def normalize_name(name)
-          if name.is_a?(Symbol)
-            name.to_s
+          base_name = name.to_s[0..-2]
+          if****@schem*****_column?(base_name)
+            __send__(:[]=, base_name, *args, &block)
           else
-            name
+            super
           end
         end
 
-        class Column
-          attr_reader :name
-          def initialize(name, options)
-            @name = name
-            @options = options
-          end
-
-          def type
-            @options[:type] || "Text"
-          end
-
-          def have_index?
-            @options[:index]
-          end
+        def respond_to_missing?(name, include_private)
+          return super unless name.to_s.end_with?("=")
 
-          def have_full_text_search_index?
-            have_index? and @options[:index_type] == :full_text_search
-          end
+          base_name = name.to_s[0..-2]
+          @schema.have_column?(base_name)
         end
       end
     end

  Deleted: lib/groonga/client/searcher/source_definition.rb (+0 -30) 100644
===================================================================
--- lib/groonga/client/searcher/source_definition.rb    2016-03-23 14:44:43 +0900 (7a4e4d2)
+++ /dev/null
@@ -1,30 +0,0 @@
-# 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
-
-module Groonga
-  class Client
-    class Searcher
-      class SourceDefinition
-        attr_reader :model_class
-        attr_reader :columns
-        def initialize(model_class, columns)
-          @model_class = model_class
-          @columns = columns
-        end
-      end
-    end
-  end
-end

  Modified: test/fixtures/rails4-mongoid/app/models/post.rb (+5 -1)
===================================================================
--- test/fixtures/rails4-mongoid/app/models/post.rb    2016-03-23 14:44:43 +0900 (b9ef812)
+++ test/fixtures/rails4-mongoid/app/models/post.rb    2016-03-23 14:51:40 +0900 (a995120)
@@ -4,5 +4,9 @@ class Post
   field :title, type: String
   field :body, type: String
 
-  PostsSearcher.add_source(self, columns: [:title, :body, :updated_at])
+  PostsSearcher.source(self).title = :title
+  PostsSearcher.source(self).body = lambda do |model|
+    model.body.gsub(/<.*?>/, "")
+  end
+  PostsSearcher.source(self).updated_at = true
 end

  Added: test/fixtures/rails4-mongoid/test/searchers/posts_searcher_test.rb (+16 -0) 100644
===================================================================
--- /dev/null
+++ test/fixtures/rails4-mongoid/test/searchers/posts_searcher_test.rb    2016-03-23 14:51:40 +0900 (a6c09d1)
@@ -0,0 +1,16 @@
+require 'test_helper'
+
+class PostsSearcherTest < ActionController::TestCase
+  include Groonga::Client::Rails::Fixture
+
+  setup do
+    @searcher = PostsSearcher.new
+  end
+
+  test "should be untagged" do
+    create(:post, body: "<p>Hello <em>World</em></p>")
+    result_set =****@searc*****_set
+    assert_equal(["Hello World"],
+                 result_set.records.collect {|record| record["body"]})
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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