OSDN Git Service

4ba9c42846340c050ac6b020ba0956f078a71d56
[pettanr/pettanr.git] / lib / manifest / model.rb
1 require "manifest/model/attribute"
2 require "manifest/model/association"
3 require "manifest/model/list"
4
5 module Manifest
6   class Model
7     include ModelModule
8     include ListModule
9     
10     def self.manager manifest, my_manifests
11       my_manifests.map {|model_name, model_manifest|
12         self.new(manifest, model_name, model_manifest)
13       }
14     end
15     
16     @@types = {
17       'public' => PublicList, 'private' => PrivateList, 'system_resource' => SystemResourceList,
18       'has_many' => HasManyList, 'has_many_through' => HasManyThroughList, 'filter' => FilterList, 
19       'through_filter' => ThroughFilterList, 'element_filter' => ElementFilterList, 
20       'play' => PlayList
21     }
22     attr :model_name, :model_manifest
23     def initialize manifest, model_name, model_manifest
24       @manifest = manifest
25       @model_name = model_name
26       @model_manifest = model_manifest
27       self.set_default
28       self.init
29     end
30     
31     def set_default
32       @model_manifest['attributes'] ||= {}
33       @model_manifest['associations'] ||= {}
34       @model_manifest['lists'] ||= {}
35     end
36     
37     def init
38       @attributes = {}
39       @model_manifest['attributes'].each {|attribute_name, attribute_manifest|
40         raise "undefined type for models > #{@model_name} > attributes > #{attribute_name}\n" unless attribute_manifest['type']
41         @attributes[attribute_name] = Attribute.new(self, attribute_name, attribute_manifest)
42       }
43       @associations = {}
44       @model_manifest['associations'].each {|association_name, association_manifest|
45         @associations[association_name] = Association.new(self, association_name, association_manifest)
46       }
47       @lists = {}
48       @model_manifest['lists'].each {|list_name, list_manifest|
49         type = list_manifest['type']
50         raise "undefined type for models > #{@model_name} > lists > #{list_name}\n" unless type
51         list = @@types[type]
52         raise "undefined class for models > #{@model_name} > actions > #{list_name} > type >  #{type}\n" unless list
53         @lists[list_name] = list.new(self, list_name, list_manifest)
54       }
55     end
56     
57   end
58 end
59