OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / lib / manifest / manifest.rb
index ae575c9..7752099 100644 (file)
-
-module Pettanr
+module Manifest
   class Manifest
-    class Controller
-      attr :item_name, :conf, :manifest, :model_name, :actions
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-        @model_name = @conf['model_name']
-        @actions = @conf['actions'] || {}
-      end
-      
-      def list_name action_name
-        a = @actions[action_name] || {}
-        l = a['list'] || {}
-        l['list_name']
-      end
-      
-    end
-    
-    class Model
-      attr :item_name, :conf, :manifest, :attributes, :associations, :belongs_to, :has_many, :has_one
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-        
-        @attributes = @conf['attributes']
-        @associations = @conf['associations'] || {}
-        @belongs_to = @associations['belongs_to'] || {}
-        @has_many = @associations['has_many'] || {}
-        @has_one = @associations['has_one'] || {}
-      end
-      
-    end
-    
-    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
-    
+    cattr_accessor :manifest
     attr :system_resources, :magic_numbers, :select_items,
-      :controller_managers, :model_managers, :list_managers, :profiler_managers, :filer_managers, :form_managers
+      :controllers, :models, :lists, :profilers, :filers, :forms, :lists
+    # call me before load routes.rb
+    # routes.rb needs engine_resources manifest in system_resources
+    # ex. it's adding license_groups routes
     def initialize manifest
       @manifest = manifest || {}
-      @system_resources = SystemResources.new(@manifest['system_resources'])
+      @system_resources = SystemResources.new(self, SystemResources.set_default(@manifest['system_resources']))
       @magic_numbers = @manifest['magic_numbers']
       @select_items = @manifest['select_items']
-      @controllers = {}
-      @models = {}
-      @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]
-      }
     end
     
+    # managers can't initialize at application.rb
+    # call me after configured Rails Application
     def init
+      return unless defined? ::Pettanr
+      @controllers = Controller.manager(self, @manifest['controllers'])
+      @models = Model.manager(self, @manifest['models'])
+      @locals = @manifest['locals']
+      @filers = Filer.manager(self, @locals['filers'])
+      @profilers = Profiler.manager(self, @locals['profilers'])
+      @forms = Form.base_manager(self, @locals['forms'])
+      @forms.merge(Form.extend_manager(self, @locals['forms']))
+      @lists = List.manager(self, @locals['lists'])
       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
-    
+    # call after load app
     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
+      @system_resources.select_items['model_loader'].each {|name, conf|
+        model_name = conf['model_name']
+        list_name = conf['list_name'] || 'select_items'
+        caption = conf['caption']
+        list = @lists[model_name][list_name]
         @select_items['model'] ||= {}
         @select_items['model'][name] = list.items.map {|item| [item.caption, item.id]}
       }
     end
     
     def add_action
+        return
       @controllers.each do |controller_name, controller|
         model_name = controller.model_name
         next if model_name.blank?
@@ -188,31 +55,23 @@ module Pettanr
       end
     end
     
-    def controller item_name
-      @controllers[item_name] || @mcontrollers[item_name.tableize]
-    end
-    
-    def model item_name
-      @models[item_name] || @models[item_name.tableize]
-    end
-    
-    def list item_name
-      @lists[item_name] || @lists[item_name.tableize]
-    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 json
+      Manifest.manifest = Manifest.new 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