OSDN Git Service

Item Dynamic ClassMethods
[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
6 module Manifest
7   class Model
8     
9     def self.manager manifest, my_manifests
10       models = {}
11       my_manifests.each {|model_name, model_manifest|
12         models[model_name] = self.new(manifest, model_name, model_manifest)
13       }
14       models
15     end
16     
17     attr :model_name, :model_manifest, 
18       :attributes, :associations, :tree, :lists
19     def initialize manifest, model_name, model_manifest
20       @manifest = manifest
21       @model_name = model_name
22       @model_manifest = model_manifest
23       self.set_default
24       self.init
25     end
26     
27     def set_default
28       @model_manifest['attributes'] ||= {}
29       @model_manifest['associations'] ||= {}
30       @model_manifest['tree'] ||= {}
31       @model_manifest['lists'] ||= {}
32       @model_manifest['attributes']['id'] = {
33         'type' => 'number',
34         'primary_key' => 1,
35         'rules' => {
36           'number' => true
37         }
38       }
39       @model_manifest['attributes']['created_at'] = {
40         'type' => 'datetime',
41       }
42       @model_manifest['attributes']['updated_at'] = {
43         'type' => 'datetime',
44       }
45     end
46     
47     def init
48       @attributes = {}
49       @model_manifest['attributes'].each {|attribute_name, attribute_manifest|
50         @attributes[attribute_name] = ModelModule::Attribute.new(self, attribute_name, attribute_manifest)
51       }
52       @associations = ModelModule::Association.new(self, @model_manifest['associations'])
53       @tree = {}
54       @model_manifest['tree'].each {|tree_name, parent_model_name|
55         @tree[tree_name] = ModelModule::Tree.new(self, tree_name, parent_model_name)
56       }
57       @lists = {}
58       @model_manifest['lists'].each {|list_name, list_manifest|
59         @lists[list_name] = ModelModule::ListFactory.factory(self, list_name, list_manifest)
60       }
61     end
62     
63     def classify
64       ::Manifest.item_name_to_model @model_name
65     end
66     
67     def valid_encode_columns
68       r = []
69       @attributes.each {|attribute_name, attribute|
70         next unless attribute.type == 'text'
71         r << attribute_name
72       }
73       r
74     end
75     
76     def owner_type
77       return :author if @attributes['author_id']
78       return :artist if @attributes['artist_id']
79       false
80     end
81     
82     def content_model
83       return true if self.owner_type
84       false
85     end
86     
87   end
88 end
89