OSDN Git Service

fix remove tree
[pettanr/pettanr.git] / lib / manifest / model / association.rb
index 6a806b1..b9e101c 100644 (file)
@@ -1,46 +1,51 @@
-require_dependency "manifest/model/association/belongs_to"
-require_dependency "manifest/model/association/has_many"
-require_dependency "manifest/model/association/has_one"
+ManifestBase.require_modules "manifest/model/association/", 
+  %w|belongs_to has_many has_one|
+
 module Manifest
   module ModelModule
-    class Association
-      attr :model, :association_name, :association_manifest, 
-        :belongs_to, :has_many, :has_one
-      def initialize model, association_name, association_manifest
-        @model = model
-        @association_name = association_name
-        @association_manifest = association_manifest
-        self.set_default
-        self.init
-      end
+    class Association < ManifestBase::Values
+      include AssociationModule
+      
+      attr :belongs_to, :has_many, :has_one
       
       def set_default
-        @association_manifest['belongs_to'] ||= {}
-        @association_manifest['has_many'] ||= {}
-        @association_manifest['has_one'] ||= {}
+        super
+        @values['belongs_to'] ||= {}
+        @values['has_many'] ||= {}
+        @values['has_one'] ||= {}
       end
       
       def init
-        @belongs_to = {}
-        @has_many = {}
-        @has_one = {}
-        @association_manifest['belongs_to'].each {|belongs_to_name, belongs_to_manifest|
-          @belongs_to[belongs_to_name] = BelongsTo.new(self, belongs_to_name, belongs_to_manifest)
-        }
-        @association_manifest['has_many'].each {|has_many_name, has_many_manifest|
-          @has_many[has_many_name] = HasMany.new(self, has_many_name, has_many_manifest)
-        }
-        @association_manifest['has_one'].each {|has_one_name, has_one_manifest|
-          @has_one[has_one_name] = HasOne.new(self, has_one_name, has_one_manifest)
-        }
+        super
+        @belongs_to = ManifestBase.load_name_values self, @values, 'belongs_to', BelongsTo
+        @has_many = ManifestBase.load_name_values self, @values, 'has_many', HasMany
+        @has_one = ManifestBase.load_name_values self, @values, 'has_one', HasOne
       end
       
       def model_name
-        @model.model_name
+        @parent.name
+      end
+      
+      def each_parent_model
+        @belongs_to.each do |name, belongs_to_manifest|
+          next unless @parent.classify.element?
+          if @parent.classify.my_peta.type == 'element'
+            next unless belongs_to_manifest.model.my_peta.type == 'root'
+          end
+          if @parent.classify.my_peta.type == 'leaf'
+            next unless belongs_to_manifest.model.my_peta.type == 'binder'
+          end
+ p  [@parent.classify  , belongs_to_manifest.model]
+          yield belongs_to_manifest.model
+        end
       end
       
-      def model_manifest
-        @model.model_manifest
+      def child_element_name child_model
+        if @has_one[child_model.item_name]
+          child_model.item_name
+        else
+          child_model.table_name
+        end
       end
       
     end