OSDN Git Service

fix:balloon edit
[pettanr/pettanr.git] / lib / manifest / manifest.rb
index 0fb89ca..ce33eea 100644 (file)
-
-module Pettanr
-  module Manifest
-    class List
-      attr :item_name, :conf, :manifest, :default_page_size, :lists
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-        @default_page_size = @conf['default_page_size'] || 20
-        @lists = @conf['lists'] || {}
-      end
-      
-    end
-    
-    class SystemResources
-      attr :conf, :license_groups, :elements, :speech_balloon_templates, :writing_formats
-      def initialize system_resource_conf
-        @conf = system_resource_conf || {}
-        @engine_resources = @conf['engine_resources'] || {}
-        @license_groups = @engine_resources['license_groups'] || {}
-        @speech_balloon_templates = @engine_resources['speech_balloon_templates'] || {}
-        @writing_formats = @engine_resources['writing_formats'] || {}
-        @elements = @conf['elements'] || {}
-      end
-      
-    end
-    
-    class Profiler
-      attr :item_name, :conf, :manifest, :columns, :lists, :belongs_to, :has_many, :has_one
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-        @columns = @conf['columns'] || {}
-        @lists = @conf['lists'] || {}
-        @associations_conf = @conf['associations'] || {}
-        @belongs_to = @associations_conf['belongs_to'] || []
-        @has_many = @associations_conf['has_many'] || []
-        @has_one = @associations_conf['has_one'] || []
-      end
-      
-    end
-    
-    class Filer
-      attr :item_name, :conf, :manifest, :icon, :symbol, :caption, :summary, :date, :edit
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-        @symbol = @conf['symbol']
-        @caption = @conf['caption']
-        @summary = @conf['summary']
-        @icon = @conf['icon']
-        @date = @conf['date']
-        @edit = @conf['edit']
-      end
-      
-    end
-    
-    class Form
-      attr :item_name, :conf, :manifest, :base, :fields, :field_names
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-        @base = @conf['base']
-        @fields = @conf['fields'] || {}
-        @field_names = @conf['field_names'] || []
-      end
-      
-    end
-    
+module Manifest
   class Manifest
-    attr :system_resources, :magic_numbers, :select_items,
-      :controller_managers, :model_managers, :list_managers, :profiler_managers, :filer_managers, :form_managers
-    def initialize manifest
-      @manifest = manifest || {}
-      @system_resources = SystemResources.new(@manifest['system_resources'])
-      @magic_numbers = @manifest['magic_numbers']
-      @select_items = @manifest['select_items']
-      @controllers = Controller.set_default(@manifest['controllers'] || {})
-      @models = Model.set_default(@manifest['models'] || {})
-      @lists = List.set_default(@manifest['lists'] || {})
-      @profilers = {}
-      @filers = {}
-      @forms = {}
-      @controller_managers = {}
-      @model_managers = {}
-      @list_managers = {}
-      @profiler_managers = {}
-      @filer_managers = {}
-      @form_managers = {}
-      (@manifest['controllers'] || {}).each {|item_name, controller_conf|
-        @controllers[item_name] = Controller.new(item_name, controller_conf, self) 
-        @controller_managers[item_name] = Pettanr::ControllerManager.new @controllers[item_name]
-      }
-      (@manifest['models'] || {}).each {|item_name, model_conf|
-        @models[item_name] = Model.new(item_name, model_conf, self) 
-        @model_managers[item_name] = Pettanr::ModelManager.new @models[item_name]
-      }
-      (@manifest['lists'] || {}).each {|item_name, list_conf|
-        @lists[item_name] = List.new(item_name, list_conf, self) 
-        @list_managers[item_name] = Pettanr::ListManager.new @lists[item_name]
-      }
-      @locals = @manifest['locals'] || {}
-      (@locals['profilers'] || {}).each {|item_name, profiler_conf|
-        @profilers[item_name] = Profiler.new item_name, profiler_conf, self
-        @profiler_managers[item_name] = Pettanr::ProfilerManager.new @profilers[item_name]
-      }
-      (@locals['filers'] || {}).each {|item_name, filer_conf|
-        @filers[item_name] = Filer.new item_name, filer_conf, self
-        @filer_managers[item_name] = Pettanr::FilerManager.new @filers[item_name]
-      }
-      (@locals['forms'] || {}).each {|form_name, form_conf|
-        @forms[form_name] = Form.new form_name, form_conf, self
-        @form_managers[form_name] = Pettanr::FormManager.new @forms[form_name]
-      }
+    cattr_accessor :manifest
+    attr :system_resources, :magic_numbers,
+      :items, :controllers, :models, :module_names, 
+      :inflectors, :singulars, :plurals
+    # call me before load routes.rb
+    # routes.rb needs engine_resources manifest in system_resources
+    # ex. it's adding license_groups routes
+    def initialize global_json
+      @module_names = []
+      @global_json = global_json || {}
+      @system_resources = SystemResource.new(self, SystemResource.set_default(@global_json['system_resources']))
+      @magic_numbers = @global_json['magic_numbers']
     end
     
     def init
-      select_items_loader
-      select_items_font_size
-      add_action
-    end
-    
-    #oh my ...
-    #JSON.stringify couldn't print 1.0 
-    # 1 == 1.0 ?
-    #change class
-    def select_items_font_size
-      (@select_items['magic_number']['speech_font_size_items']).each {|conf|
-        conf[1] = conf[1].to_f 
-      }
-    end
-    
-    def select_items_loader
-      (@select_items['model_loader'] || {}).each {|name, conf|
-        list = Pettanr::Application::manifest.list_managers[conf['model']].open conf['list_name'], nil, nil, nil
-        @select_items['model'] ||= {}
-        @select_items['model'][name] = list.items.map {|item| [item.caption, item.id]}
-      }
-    end
-    
-    def add_action
-      @controllers.each do |controller_name, controller|
-        model_name = controller.model_name
-        next if model_name.blank?
-        controller.actions.each do |action_name, conf|
-          next unless conf['type'] == 'list'
-          list_name = conf['list']['list_name']
-          list = Pettanr::Application::manifest.list_managers[model_name]
-          list.add_action action_name, list_name
+      # managers can't initialize before load  application.rb
+      return unless defined? ::Pettanr
+      @inflectors = @global_json['inflectors']
+      @item_names = {}
+      @table_names = {}
+      @inflectors.each do |item_name, table_name|
+        @item_names[item_name] = table_name  # monkey copy
+        @table_names[table_name] = item_name
+      end
+      @items = ManifestBase.load_type_name_args(self, @global_json, 'items', ItemFactory)
+      @controllers = Controller.load(self, @global_json, 'controllers')
+      self.replace_action_alias
+      @models = Model.load(self, @global_json, 'models')
+    end
+    
+    def init_after_load_manifest
+      @system_resources.init
+      PettanImager.tmb_w = @magic_numbers['thumbnail_width']
+      PettanImager.tmb_h = @magic_numbers['thumbnail_height']
+    end
+    
+    def replace_action_alias
+      replaces = {}
+      @controllers.each do |controller_name, controller_manifest|
+        controller_manifest.actions.each do |action_name, action_manifest|
+          next unless action_manifest.alias
+          alias_action = action_manifest.alias_action
+          next unless alias_action.type == action_manifest.type
+          args = {}
+          action_manifest.a_arg_names.each do |name|
+            args[name] = action_manifest.__send__(name) || alias_action.__send__(name)
+          end
+          action_manifest.b_arg_names.each do |name|
+            args[name] = alias_action.__send__(name) || action_manifest.__send__(name)
+          end
+          args['original'] = action_manifest
+          json = {'type' => alias_action.type, 'args' => args}
+          new_action_manifest = ControllerModule::ActionFactory.factory(
+            controller_manifest, action_manifest.name, json, action_manifest.module_name
+          )
+          replaces[controller_name] ||= {}
+          replaces[controller_name][action_name] = new_action_manifest
+        end
+      end
+      replaces.each do |controller_name, controller|
+        controller.each do |action_name, action_manifest|
+          ::Manifest.manifest.controllers[controller_name].actions[action_name] = action_manifest
         end
       end
     end
     
-    def controller item_name
-      @controllers[item_name] || @mcontrollers[item_name.tableize]
+    def load_models_manifest
+      @models.each do |model_name, model|
+        # skip extend model.
+        next unless @items[model.name]
+        model.classify.load_manifest
+      end
     end
     
-    def model item_name
-      @models[item_name] || @models[item_name.tableize]
+    # table_name to item_name
+    def singularize table_name
+      @table_names[table_name]
     end
     
-    def list item_name
-      @lists[item_name] || @lists[item_name.tableize]
+    # item_name to table_name
+    def pluralize item_name
+      @item_names[item_name]
     end
     
-    def profiler item_name
-      @profilers[item_name] || @profilers[item_name.tableize]
+  end
+  
+  module ModuleMethods
+    def manifest
+      Manifest.manifest
     end
     
-    def filer item_name
-      @filers[item_name] || @filers[item_name.tableize]
+    def load global_json
+      Manifest.manifest = Manifest.new global_json
     end
     
-    def form form_name
-      @forms[form_name] || @forms[form_name.tableize]
+    def item_name_to_model item_name
+      item_name.classify.constantize
     end
     
   end
-  
+
+  extend ModuleMethods
 end