X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=lib%2Fmanifest%2Fmanifest.rb;h=7752099e8bab315dd32dadc0e1a4f4a9c24c467a;hb=4d910560a32d41372815a67f79a654b6352fd679;hp=bf0c08d5efa7017f126646e31fd286a46e505f7a;hpb=32011a831d9b226af0ee586c91ca5c5225048976;p=pettanr%2Fpettanr.git diff --git a/lib/manifest/manifest.rb b/lib/manifest/manifest.rb index bf0c08d5..7752099e 100644 --- a/lib/manifest/manifest.rb +++ b/lib/manifest/manifest.rb @@ -1,204 +1,77 @@ - -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 - 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 - } + add_action 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 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] + 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 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