OSDN Git Service

add peta model_manifest
[pettanr/pettanr.git] / lib / manifest / model.rb
1 require_dependency "manifest/model/attribute"
2 require_dependency "manifest/model/association"
3 require_dependency "manifest/model/list"
4 require_dependency "manifest/model/tree"
5 require_dependency "manifest/model/peta"
6
7 module Manifest
8   class Model
9     
10     def self.manager manifest, my_manifests
11       models = {}
12       my_manifests.each {|model_name, model_manifest|
13         models[model_name] = self.new(manifest, model_name, model_manifest)
14       }
15       models
16     end
17     
18     attr :model_name, :model_manifest, 
19       :attributes, :associations, :tree, :lists, :peta, :extend_column_name
20     def initialize manifest, model_name, model_manifest
21       @manifest = manifest
22       @model_name = model_name
23       @model_manifest = model_manifest
24       self.set_default
25       self.init
26     end
27     
28     def set_default
29       @model_manifest['attributes'] ||= {}
30       @model_manifest['associations'] ||= {}
31       @model_manifest['tree'] ||= {}
32       @model_manifest['lists'] ||= {}
33       @model_manifest['extend_column_name'] ||= 'classname'
34       @model_manifest['peta'] ||= {}
35       @model_manifest['attributes']['id'] = {
36         'type' => 'number',
37         'primary_key' => 1,
38         'rules' => {
39           'number' => true
40         }
41       }
42       @model_manifest['attributes']['created_at'] = {
43         'type' => 'datetime',
44       }
45       @model_manifest['attributes']['updated_at'] = {
46         'type' => 'datetime',
47       }
48     end
49     
50     def init
51       @attributes = {}
52       @model_manifest['attributes'].each {|attribute_name, attribute_manifest|
53         @attributes[attribute_name] = ModelModule::Attribute.new(self, attribute_name, attribute_manifest)
54       }
55       @associations = ModelModule::Association.new(self, @model_manifest['associations'])
56       @tree = {}
57       @model_manifest['tree'].each {|tree_name, parent_model_name|
58         @tree[tree_name] = ModelModule::Tree.new(self, tree_name, parent_model_name)
59       }
60       @peta = ModelModule::PetaFactory.factory(self, @model_manifest['peta'])
61       @extend_column_name = @model_manifest['extend_column_name']
62       @lists = {}
63       @model_manifest['lists'].each {|list_name, list_manifest|
64         @lists[list_name] = ModelModule::ListFactory.factory(self, list_name, list_manifest)
65       }
66     end
67     
68     def classify
69       ::Manifest.item_name_to_model @model_name
70     end
71     
72     def table_name
73       self.classify.table_name
74     end
75     
76     def valid_encode_columns
77       r = []
78       @attributes.each {|attribute_name, attribute|
79         next unless attribute.type == 'text'
80         r << attribute_name
81       }
82       r
83     end
84     
85     def owner_type
86       return :author if @attributes['author_id']
87       return :artist if @attributes['artist_id']
88       false
89     end
90     
91     def content_model
92       return true if self.owner_type
93       false
94     end
95     
96     def child_model_manifests tree_name
97       r = []
98       ::Manifest.manifest.models.each {|child_model_name, child_model_manifest|
99         next unless child_model_manifest.tree[tree_name]
100         next unless child_model_manifest.tree[tree_name].parent_model_name == @model_name
101         r << child_model_manifest
102       }
103       r
104     end
105     
106     def child_models tree_name
107       self.child_model_manifests(tree_name).map {|child_model_manifest|
108         child_model_manifest.classify
109       }
110     end
111     
112     def child_element_names tree_name
113       self.child_models(tree_name).map {|child_model|
114         self.associations.child_element_name(child_model)
115       }
116     end
117     
118     def child_element_name item_name
119       self.associations.child_element_name(child_model_manifest)
120     end
121     
122   end
123 end
124