[Groonga-commit] ranguba/groonga-client-model at 6cb42ba [master] Support dynamic column and nested column values

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Dec 19 12:25:47 JST 2016


Kouhei Sutou	2016-12-19 12:25:47 +0900 (Mon, 19 Dec 2016)

  New Revision: 6cb42baeab24b5d070b27859bf67a3fa755fb1fb
  https://github.com/ranguba/groonga-client-model/commit/6cb42baeab24b5d070b27859bf67a3fa755fb1fb

  Message:
    Support dynamic column and nested column values

  Modified files:
    lib/groonga_client_model/modelize.rb
    lib/groonga_client_model/record.rb
    lib/groonga_client_model/schema.rb
    test/apps/rails5/Gemfile.lock

  Modified: lib/groonga_client_model/modelize.rb (+44 -2)
===================================================================
--- lib/groonga_client_model/modelize.rb    2016-12-12 16:28:02 +0900 (842b9f1)
+++ lib/groonga_client_model/modelize.rb    2016-12-19 12:25:47 +0900 (3b7b55a)
@@ -32,11 +32,53 @@ module GroongaClientModel
     end
 
     def records
-      @modelized_records ||= super.collect do |raw_record|
-        record = @model_class.new(raw_record)
+      @modelized_records ||= build_records(super)
+    end
+
+    private
+    def build_records(raw_records)
+      columns = @model_class.columns
+      raw_records.collect do |raw_record|
+        attributes, dynamic_attributes = build_attributes(columns, raw_record)
+        record = @model_class.new(attributes)
         record.instance_variable_set(:@new_record, false)
+        record.assign_dynamic_attributes(dynamic_attributes)
         record
       end
     end
+
+    def build_attributes(columns, raw_record)
+      attributes = {}
+      dynamic_attributes = {}
+      raw_record.each do |name, value|
+        primary_name, sub_name = name.split(".", 2)
+        if sub_name.nil?
+          if columns.exist?(primary_name)
+            if attributes.key?(primary_name)
+              value = attributes[primary_name].merge("_key" => value)
+            end
+            attributes[primary_name] = value
+          else
+            dynamic_attributes[primary_name] = value
+          end
+        else
+          if columns.exist?(primary_name)
+            if attributes.key?(primary_name)
+              unless attributes[primary_name].is_a?(Hash)
+                attributes[primary_name] = {
+                  "_key" => attributes[primary_name],
+                }
+              end
+            else
+              attributes[primary_name] = {}
+            end
+            attributes[primary_name][sub_name] = value
+          else
+            dynamic_attributes[name] = value
+          end
+        end
+      end
+      [attributes, dynamic_attributes]
+    end
   end
 end

  Modified: lib/groonga_client_model/record.rb (+17 -0)
===================================================================
--- lib/groonga_client_model/record.rb    2016-12-12 16:28:02 +0900 (dbf4307)
+++ lib/groonga_client_model/record.rb    2016-12-19 12:25:47 +0900 (cb0bf0a)
@@ -176,6 +176,23 @@ module GroongaClientModel
       true
     end
 
+    def assign_dynamic_attributes(dynamic_attributes)
+      return if dynamic_attributes.blank?
+
+      dynamic_attributes.each do |name, value|
+        assign_dynamic_attribute(name, value)
+      end
+    end
+
+    def assign_dynamic_attribute(name, value)
+      if respond_to?(name)
+        singleton_class.__send__(:undef_method, name)
+      end
+      singleton_class.__send__(:define_method, name) do
+        value
+      end
+    end
+
     private
     def upsert
       Client.open do |client|

  Modified: lib/groonga_client_model/schema.rb (+4 -0)
===================================================================
--- lib/groonga_client_model/schema.rb    2016-12-12 16:28:02 +0900 (77461db)
+++ lib/groonga_client_model/schema.rb    2016-12-19 12:25:47 +0900 (7eabc65)
@@ -87,6 +87,10 @@ module GroongaClientModel
         @raw_columns = raw_columns
       end
 
+      def exist?(name)
+        @raw_columns.key?(name)
+      end
+
       def names
         @raw_columns.keys
       end

  Modified: test/apps/rails5/Gemfile.lock (+1 -1)
===================================================================
--- test/apps/rails5/Gemfile.lock    2016-12-12 16:28:02 +0900 (bc38bc3)
+++ test/apps/rails5/Gemfile.lock    2016-12-19 12:25:47 +0900 (0ccfe0b)
@@ -1,7 +1,7 @@
 PATH
   remote: ../../../../groonga-client
   specs:
-    groonga-client (0.3.4)
+    groonga-client (0.3.5)
       gqtp (>= 1.0.4)
       groonga-command (>= 1.2.8)
       hashie
-------------- next part --------------
HTML����������������������������...
Descargar 



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