OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / lib / manifest / manifest.rb
index b15e428..7752099 100644 (file)
-
-module Pettanr
+module Manifest
   class Manifest
-    class Model
-      attr :item_name, :conf, :manifest, :attributes, :associations
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-      end
-      
-      def attributes
-        @model_manifest['attributes'] || {}
-      end
-      
-      def associations
-        @conf['name'] || {}
-      end
-      
-    end
-    
-    class List
-      attr :item_name, :conf, :manifest
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-      end
-      
-    end
-    
-    class License
-      attr :conf
-      def initialize manifest
-        @manifest = manifest
-        @conf = @manifest['licenses'] || {}
-      end
-    end
-    
-    class Element
-      attr :conf
-      def initialize manifest
-        @manifest = manifest
-        @conf = @manifest['elements'] || {}
-      end
-    end
-    
-    class SpeechBalloonTemplate
-      attr :conf
-      def initialize manifest
-        @manifest = manifest
-        @conf = @manifest['speech_balloon_templates'] || {}
-      end
-    end
-    
-    class WritingFormat
-      attr :conf
-      def initialize manifest
-        @manifest = manifest
-        @conf = @manifest['writing_formats'] || {}
-      end
-    end
-    
-    class Profile
-      attr :conf
-      def initialize manifest
-        @manifest = manifest
-        @conf = @manifest['profile'] || {}
-      end
-    end
-    
-    class Profiler
-      attr :item_name, :conf, :manifest, :columns, :belongs_to, :has_many, :has_one
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-        @columns = @conf['columns'] || {}
-        @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, :icon, :symbol, :caption, :summary, :date, :edit
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-      end
-      
-    end
-    
-    attr :licenses, :elements, :speech_balloon_templates, :writing_formats, :profile,
-      :model_managers, :list_managers, :profiler_managers, :filer_managers, :form_managers
+    cattr_accessor :manifest
+    attr :system_resources, :magic_numbers, :select_items,
+      :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 || {}
-      @models = {}
-      @lists = {}
-      @licenses = License.new(manifest) 
-      @elements = Element.new(manifest)
-      @speech_balloon_templates = SpeechBalloonTemplate.new(manifest)
-      @writing_formats = WritingFormat.new(manifest)
-      @profile = Profile.new(manifest)
-      @profilers = {}
-      @filers = {}
-      @forms = {}
-      @model_managers = {}
-      @list_managers = {}
-      @profiler_managers = {}
-      @filer_managers = {}
-      @form_managers = {}
-      (@manifest['models'] || {}).each {|item_name, model_conf|
-        @models[item_name] = Model.new(item_name, model_conf, manifest) 
-        @model_managers[item_name] = Pettanr::ModelManager.new @models[item_name]
+      @system_resources = SystemResources.new(self, SystemResources.set_default(@manifest['system_resources']))
+      @magic_numbers = @manifest['magic_numbers']
+      @select_items = @manifest['select_items']
+    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
+      add_action
+    end
+    
+    # call after load app
+    def select_items_loader
+      @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]}
       }
-      (@manifest['lists'] || {}).each {|item_name, list_conf|
-        @lists[item_name] = List.new(item_name, list_conf, manifest) 
-        @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, manifest
-        @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, manifest
-        @filer_managers[item_name] = Pettanr::FilerManager.new @filers[item_name]
-      }
-      (@locals['forms'] || {}).each {|item_name, form_conf|
-        @forms[item_name] = Form.new item_name, form_conf, manifest
-        @form_managers[item_name] = Pettanr::FormManager.new @forms[item_name]
-      }
-    end
-    
-    def model item_name
-      @models[item_name] || @models[item_name.tableize]
     end
     
-    def list item_name
-      @lists[item_name] || @lists[item_name.tableize]
+    def add_action
+        return
+      @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
+        end
+      end
     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 item_name
-      @forms[item_name] || @forms[item_name.tableize]
+    def item_name_to_model item_name
+      item_name.classify.constantize
     end
     
   end
-  
+
+  extend ModuleMethods
 end