OSDN Git Service

fix filer
[pettanr/pettanr.git] / lib / manifest / manifest.rb
index c5f247d..a871faa 100644 (file)
-
-module Pettanr
+module Manifest
   class Manifest
-    class Model
-      attr :conf, :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 :conf
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-      end
-      
+    cattr_accessor :manifest
+    attr :system_resources, :magic_numbers,
+      :controllers, :models, :lists, :profilers, :filers, :forms, :lists, :views
+    # 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 = SystemResource.new(self, SystemResource.set_default(@manifest['system_resources']))
+      @magic_numbers = @manifest['magic_numbers']
     end
     
-    class Profiler
-      attr :conf, :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
-      
+    def init
+      # managers can't initialize before load  application.rb
+      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'])
+      @system_resources.init
+      add_action
     end
     
-    class Filer
-      attr :conf, :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']
+    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
     
-    class Form
-      attr :conf, :icon, :symbol, :caption, :summary, :date, :edit
-      def initialize item_name, conf, manifest
-        @item_name = item_name
-        @conf = conf || {}
-        @manifest = manifest
-      end
-      
-    end
-    
-    def initialize manifest
-      @manifest = manifest || {}
-      @models = {}
-      @lists = {}
-      @profilers = {}
-      @filers = {}
-      @forms = {}
-      manifest['models'].each {|item_name, model_conf|
-        @models[item_name] = Model.new(item_name, model_conf, manifest) 
-      }
-      manifest['lists'].each {|item_name, list_manifest|
-        @lists[item_name] = List.new(item_name, list_manifest) 
-      }
-      manifest['locals']['profilers'].each {|item_name, profiler_manifest|
-        @profilers[item_name] = Profiler.new item_name, profiler_manifest
-      }
-      manifest['locals']['filers'].each {|item_name, filer_manifest|
-        @filers[item_name] = Filer.new item_name, filer_manifest
-      }
-      manifest['locals']['forms'].each {|item_name, form_manifest|
-        @forms[item_name] = Form.new item_name, form_manifest
-      }
-    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
+  
+  module ModuleMethods
+    def manifest
+      Manifest.manifest
     end
     
-    def profiler item_name
-      @profilers[item_name] || @profilers[item_name.tableize]
+    def load json
+      Manifest.manifest = Manifest.new json
     end
     
-    def filer item_name
-      @filers[item_name] || @filers[item_name.tableize]
+    def item_name_to_model item_name
+      item_name.classify.constantize
     end
     
-    def form item_name
-      @forms[item_name] || @forms[item_name.tableize]
+    def singularize name
+      name.singularize
     end
     
   end
-  
+
+  extend ModuleMethods
 end