OSDN Git Service

fix extend_column_name
[pettanr/pettanr.git] / lib / manifest / model.rb
index 4ba9c42..cdeb2a5 100644 (file)
@@ -1,25 +1,21 @@
-require "manifest/model/attribute"
-require "manifest/model/association"
-require "manifest/model/list"
+require_dependency "manifest/model/attribute"
+require_dependency "manifest/model/association"
+require_dependency "manifest/model/list"
+require_dependency "manifest/model/tree"
 
 module Manifest
   class Model
-    include ModelModule
-    include ListModule
     
     def self.manager manifest, my_manifests
-      my_manifests.map {|model_name, model_manifest|
-        self.new(manifest, model_name, model_manifest)
+      models = {}
+      my_manifests.each {|model_name, model_manifest|
+        models[model_name] = self.new(manifest, model_name, model_manifest)
       }
+      models
     end
     
-    @@types = {
-      'public' => PublicList, 'private' => PrivateList, 'system_resource' => SystemResourceList,
-      'has_many' => HasManyList, 'has_many_through' => HasManyThroughList, 'filter' => FilterList, 
-      'through_filter' => ThroughFilterList, 'element_filter' => ElementFilterList, 
-      'play' => PlayList
-    }
-    attr :model_name, :model_manifest
+    attr :model_name, :model_manifest, 
+      :attributes, :associations, :tree, :lists, :extend_column_name
     def initialize manifest, model_name, model_manifest
       @manifest = manifest
       @model_name = model_name
@@ -31,27 +27,92 @@ module Manifest
     def set_default
       @model_manifest['attributes'] ||= {}
       @model_manifest['associations'] ||= {}
+      @model_manifest['tree'] ||= {}
       @model_manifest['lists'] ||= {}
+      @model_manifest['extend_column_name'] ||= 'classname'
+      @model_manifest['attributes']['id'] = {
+        'type' => 'number',
+        'primary_key' => 1,
+        'rules' => {
+          'number' => true
+        }
+      }
+      @model_manifest['attributes']['created_at'] = {
+        'type' => 'datetime',
+      }
+      @model_manifest['attributes']['updated_at'] = {
+        'type' => 'datetime',
+      }
     end
     
     def init
       @attributes = {}
       @model_manifest['attributes'].each {|attribute_name, attribute_manifest|
-        raise "undefined type for models > #{@model_name} > attributes > #{attribute_name}\n" unless attribute_manifest['type']
-        @attributes[attribute_name] = Attribute.new(self, attribute_name, attribute_manifest)
+        @attributes[attribute_name] = ModelModule::Attribute.new(self, attribute_name, attribute_manifest)
       }
-      @associations = {}
-      @model_manifest['associations'].each {|association_name, association_manifest|
-        @associations[association_name] = Association.new(self, association_name, association_manifest)
+      @associations = ModelModule::Association.new(self, @model_manifest['associations'])
+      @tree = {}
+      @model_manifest['tree'].each {|tree_name, parent_model_name|
+        @tree[tree_name] = ModelModule::Tree.new(self, tree_name, parent_model_name)
       }
+      @extend_column_name = @model_manifest['extend_column_name']
       @lists = {}
       @model_manifest['lists'].each {|list_name, list_manifest|
-        type = list_manifest['type']
-        raise "undefined type for models > #{@model_name} > lists > #{list_name}\n" unless type
-        list = @@types[type]
-        raise "undefined class for models > #{@model_name} > actions > #{list_name} > type >  #{type}\n" unless list
-        @lists[list_name] = list.new(self, list_name, list_manifest)
+        @lists[list_name] = ModelModule::ListFactory.factory(self, list_name, list_manifest)
+      }
+    end
+    
+    def classify
+      ::Manifest.item_name_to_model @model_name
+    end
+    
+    def table_name
+      self.classify.table_name
+    end
+    
+    def valid_encode_columns
+      r = []
+      @attributes.each {|attribute_name, attribute|
+        next unless attribute.type == 'text'
+        r << attribute_name
+      }
+      r
+    end
+    
+    def owner_type
+      return :author if @attributes['author_id']
+      return :artist if @attributes['artist_id']
+      false
+    end
+    
+    def content_model
+      return true if self.owner_type
+      false
+    end
+    
+    def each_child_model_manifest tree_name
+      ::Manifest.models.each {|child_model_name, child_model_manifest|
+        next unless child_model_manifest.tree[tree_name]
+        next unless child_model_manifest.tree[tree_name].parent_model_name == @model_name
+        yield child_model_manifest
+      }
+    end
+    
+    def child_models tree_name
+      r = []
+      self.each_child_model_manifest(tree_name) {|child_model_manifest|
+        r << child_model_manifest.classify
+      }
+      r
+    end
+    
+    def child_element_names tree_name
+      r = []
+      self.each_child_model_manifest(tree_name) {|child_model_manifest|
+        # has_one ?
+        r << self.associations.child_element_name(child_model_manifest.model_name)
       }
+      r
     end
     
   end