OSDN Git Service

fix:balloon edit
[pettanr/pettanr.git] / lib / manifest / manifest.rb
1 module Manifest
2   class Manifest
3     cattr_accessor :manifest
4     attr :system_resources, :magic_numbers,
5       :items, :controllers, :models, :module_names, 
6       :inflectors, :singulars, :plurals
7     # call me before load routes.rb
8     # routes.rb needs engine_resources manifest in system_resources
9     # ex. it's adding license_groups routes
10     def initialize global_json
11       @module_names = []
12       @global_json = global_json || {}
13       @system_resources = SystemResource.new(self, SystemResource.set_default(@global_json['system_resources']))
14       @magic_numbers = @global_json['magic_numbers']
15     end
16     
17     def init
18       # managers can't initialize before load  application.rb
19       return unless defined? ::Pettanr
20       @inflectors = @global_json['inflectors']
21       @item_names = {}
22       @table_names = {}
23       @inflectors.each do |item_name, table_name|
24         @item_names[item_name] = table_name  # monkey copy
25         @table_names[table_name] = item_name
26       end
27       @items = ManifestBase.load_type_name_args(self, @global_json, 'items', ItemFactory)
28       @controllers = Controller.load(self, @global_json, 'controllers')
29       self.replace_action_alias
30       @models = Model.load(self, @global_json, 'models')
31     end
32     
33     def init_after_load_manifest
34       @system_resources.init
35       PettanImager.tmb_w = @magic_numbers['thumbnail_width']
36       PettanImager.tmb_h = @magic_numbers['thumbnail_height']
37     end
38     
39     def replace_action_alias
40       replaces = {}
41       @controllers.each do |controller_name, controller_manifest|
42         controller_manifest.actions.each do |action_name, action_manifest|
43           next unless action_manifest.alias
44           alias_action = action_manifest.alias_action
45           next unless alias_action.type == action_manifest.type
46           args = {}
47           action_manifest.a_arg_names.each do |name|
48             args[name] = action_manifest.__send__(name) || alias_action.__send__(name)
49           end
50           action_manifest.b_arg_names.each do |name|
51             args[name] = alias_action.__send__(name) || action_manifest.__send__(name)
52           end
53           args['original'] = action_manifest
54           json = {'type' => alias_action.type, 'args' => args}
55           new_action_manifest = ControllerModule::ActionFactory.factory(
56             controller_manifest, action_manifest.name, json, action_manifest.module_name
57           )
58           replaces[controller_name] ||= {}
59           replaces[controller_name][action_name] = new_action_manifest
60         end
61       end
62       replaces.each do |controller_name, controller|
63         controller.each do |action_name, action_manifest|
64           ::Manifest.manifest.controllers[controller_name].actions[action_name] = action_manifest
65         end
66       end
67     end
68     
69     def load_models_manifest
70       @models.each do |model_name, model|
71         # skip extend model.
72         next unless @items[model.name]
73         model.classify.load_manifest
74       end
75     end
76     
77     # table_name to item_name
78     def singularize table_name
79       @table_names[table_name]
80     end
81     
82     # item_name to table_name
83     def pluralize item_name
84       @item_names[item_name]
85     end
86     
87   end
88   
89   module ModuleMethods
90     def manifest
91       Manifest.manifest
92     end
93     
94     def load global_json
95       Manifest.manifest = Manifest.new global_json
96     end
97     
98     def item_name_to_model item_name
99       item_name.classify.constantize
100     end
101     
102   end
103
104   extend ModuleMethods
105 end
106