OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / lib / manifest / manifest.rb
1 module Manifest
2   class Manifest
3     cattr_accessor :manifest
4     attr :system_resources, :magic_numbers, :select_items,
5       :controllers, :models, :lists, :profilers, :filers, :forms, :lists
6     # call me before load routes.rb
7     # routes.rb needs engine_resources manifest in system_resources
8     # ex. it's adding license_groups routes
9     def initialize manifest
10       @manifest = manifest || {}
11       @system_resources = SystemResources.new(self, SystemResources.set_default(@manifest['system_resources']))
12       @magic_numbers = @manifest['magic_numbers']
13       @select_items = @manifest['select_items']
14     end
15     
16     # managers can't initialize at application.rb
17     # call me after configured Rails Application
18     def init
19       return unless defined? ::Pettanr
20       @controllers = Controller.manager(self, @manifest['controllers'])
21       @models = Model.manager(self, @manifest['models'])
22       @locals = @manifest['locals']
23       @filers = Filer.manager(self, @locals['filers'])
24       @profilers = Profiler.manager(self, @locals['profilers'])
25       @forms = Form.base_manager(self, @locals['forms'])
26       @forms.merge(Form.extend_manager(self, @locals['forms']))
27       @lists = List.manager(self, @locals['lists'])
28       select_items_loader
29       add_action
30     end
31     
32     # call after load app
33     def select_items_loader
34       @system_resources.select_items['model_loader'].each {|name, conf|
35         model_name = conf['model_name']
36         list_name = conf['list_name'] || 'select_items'
37         caption = conf['caption']
38         list = @lists[model_name][list_name]
39         @select_items['model'] ||= {}
40         @select_items['model'][name] = list.items.map {|item| [item.caption, item.id]}
41       }
42     end
43     
44     def add_action
45         return
46       @controllers.each do |controller_name, controller|
47         model_name = controller.model_name
48         next if model_name.blank?
49         controller.actions.each do |action_name, conf|
50           next unless conf['type'] == 'list'
51           list_name = conf['list']['list_name']
52           list = Pettanr::Application::manifest.list_managers[model_name]
53           list.add_action action_name, list_name
54         end
55       end
56     end
57     
58   end
59   
60   module ModuleMethods
61     def manifest
62       Manifest.manifest
63     end
64     
65     def load json
66       Manifest.manifest = Manifest.new json
67     end
68     
69     def item_name_to_model item_name
70       item_name.classify.constantize
71     end
72     
73   end
74
75   extend ModuleMethods
76 end
77