[Groonga-commit] ranguba/groonga-client-model at 3ad64c2 [master] Support building sub record automatically

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Mon Dec 19 13:52:56 JST 2016


Kouhei Sutou	2016-12-19 13:52:56 +0900 (Mon, 19 Dec 2016)

  New Revision: 3ad64c21224c247f43d23fb0362dc930990c215f
  https://github.com/ranguba/groonga-client-model/commit/3ad64c21224c247f43d23fb0362dc930990c215f

  Message:
    Support building sub record automatically

  Modified files:
    lib/groonga_client_model/record.rb

  Modified: lib/groonga_client_model/record.rb (+33 -0)
===================================================================
--- lib/groonga_client_model/record.rb    2016-12-19 13:32:18 +0900 (cb0bf0a)
+++ lib/groonga_client_model/record.rb    2016-12-19 13:52:56 +0900 (78c2996)
@@ -101,6 +101,9 @@ module GroongaClientModel
 
       def define_method_attribute=(name)
         define_method("#{name}=") do |value|
+          if value.is_a?(Hash)
+            value = build_sub_record(name, value)
+          end
           @attributes[name] = value
         end
       end
@@ -231,5 +234,35 @@ module GroongaClientModel
         true
       end
     end
+
+    def build_sub_record(name, value)
+      column = self.class.columns[name]
+      return value unless column
+
+      return value unless column.value_type.type == "reference"
+
+      class_name = name.classify
+      begin
+        sub_record_class = class_name.constantize
+      rescue NameError
+        return value
+      end
+
+      is_vector = (column.type == "vector")
+      if is_vector
+        sub_record_values = []
+        value.each do |sub_name, sub_values|
+          sub_values.each_with_index do |sub_value, i|
+            sub_record_value = (sub_record_values[i] ||= {})
+            sub_record_value[sub_name] = sub_value
+          end
+        end
+        sub_record_values.collect do |sub_record_value|
+          sub_record_class.new(sub_record_value)
+        end
+      else
+        sub_record_class.new(value)
+      end
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index