OSDN Git Service

diet manifest
[pettanr/pettanr.git] / lib / manifest / profiler.rb
index 1512dbb..d58dab3 100644 (file)
-
-module Pettanr
-  class ProfilerManager
-    class Profiler
-      class Column
-        def initialize item_name, column_name, item, operators, manifest
-          @item_name = item_name
-          @column_name = column_name
-          @item = item
-          @operators = operators
-          @manifest = manifest
-          
-          @model = @item_name.classify.constantize
-          @model_attributes = @manifest.model(@item_name).attributes
-          @column_conf = @model_attributes[@column_name]
-        end
-        
-        def label view
-          @model.human_attribute_name(@column_name)
-        end
-        
-        def date?
-          case @column_conf['type']
-          when 'datetime'
-            if self.value
-              true
-            else
-              false
-            end
-          else
-            false
-          end
-        end
-        
-        def value
-          @item.attributes[@column_name]
-        end
-        
-        def disp_value view
-          if self.date?
-            view.l self.value
-          else
-            self.value
-          end
-        end
-        
-        def note?
-          if @column_conf['source']
-            case @column_conf['source']['type']
-            when 'magic_number'
-              true
-            when 'model'
-              false
-            else
-              false
-            end
-          else
-            false
-          end
-        end
-        
-        def note view
-          if self.note?
-            '(' + view.t_selected_item(@column_conf['source']['key'], self.value) + ')'
-          else
-          end
-        end
-        
-      end
-      
-      def initialize item_name, item, operators, conf, manifest
-        @item_name = item_name
-        @item = item
-        @operators = operators
-        @conf = conf
-        @manifest = manifest
-        
-        @lists = @manifest.profiler(@item_name).lists
-        @belongs_to_conf = @manifest.profiler(@item_name).belongs_to
-        @has_many_conf = @manifest.profiler(@item_name).has_many
-        @has_one_conf = @manifest.profiler(@item_name).has_one
-      end
-      
-      def modelize str
-        str.classify.constantize
-      end
-      
-      def header_filer
-        @manifest.filer_managers[@item_name].open @item_name, [@item], @operators, nil
-      end
-      
-      def each_column
-        @conf['columns'].each do |column_name|
-          column = Column.new @item_name, column_name, @item, @operators, @manifest
-          yield column
-        end
-      end
-      
-      def parent_item parent_model, model_belongs_to_conf
-        r = nil
-        id_column = model_belongs_to_conf['id_column']
-        begin
-          r = parent_model.show(@item.attributes[id_column], @operators)
-        rescue ActiveRecord::RecordNotFound
-        end
-        r
-      end
-      
-      def each_belongs_to
-        @belongs_to_conf.each do |association_model_name|
-          model_belongs_to_conf = @manifest.model(@item_name).belongs_to[association_model_name]
-          parent_model = self.modelize association_model_name
-          parent = self.parent_item parent_model, model_belongs_to_conf
-          if parent
-            parent_filer = @manifest.filer_managers[@item_name].open @item_name, [@item], @operators, nil
-            yield parent_filer
-          end
-        end
-      end
-      
-      def each_has_many
-        @has_many_conf.each do |profiler_list_name|
-          model_name = @lists['model_name']
-          list_name = @lists['list_name']
-          list = @manifest.list_managers[model_name].open(list_name, 1, 3, @operators)
-          items = list.items 
-          has_many_filer = @manifest.filer_managers[model_name].open model_name, items, @operators, nil
-          yield has_many_filer
-        end
-      end
-      
-      def each_has_one 
-        @has_one_conf.each do |profiler_list_name|
-          list = @manifest.list_managers[@item_name].open(profiler_list_name, 1, 1, @operators)
-          items = list.items 
-          has_one_filer = @manifest.filer_managers[@item_name].open @item_name, items, @operators, nil
-          yield has_one_filer
-        end
-      end
-      
+require_dependency "manifest/profiler/column"
+require_dependency "manifest/profiler/list"
+require_dependency "manifest/profiler/association"
+module Manifest
+  class Profiler
+    
+    def self.manager manifest, my_manifests
+      profilers = {}
+      my_manifests.each {|item_name, profiler_manifest|
+        profilers[item_name] = self.new(manifest, item_name, profiler_manifest)
+      }
+      profilers
     end
     
-    def initialize profiler_manifest
+    attr :profiler_manifest, :item_name, :manifest, 
+      :column_names, :lists, :associations
+    def initialize manifest, item_name, profiler_manifest
+      @manifest = manifest
+      @item_name = item_name
       @profiler_manifest = profiler_manifest
-      @item_name = @profiler_manifest.item_name
-      @manifest = profiler_manifest.manifest
-      @profiler_conf = @profiler_manifest.conf
+      self.set_default
+      self.init
+    end
+    
+    def set_default
+      @profiler_manifest['column_names'] ||= []
+      @profiler_manifest['lists'] ||= {}
+      @profiler_manifest['associations'] ||= {}
+    end
+    
+    def init
+      @column_names = ['id'] + @profiler_manifest['column_names'] + ['created_at', 'updated_at']
+      @lists = {}
+      @profiler_manifest['lists'].each {|list_name, list_manifest|
+        @lists[list_name] = ProfilerModule::List.new(self, list_name, list_manifest)
+      }
+      @associations = ProfilerModule::Association.new(self, @profiler_manifest['associations'])
     end
     
-    def open item, operators
-      Profiler.new @item_name, item, operators, @profiler_conf, @manifest
+    def each_column
+      @column_names
     end
     
   end