orbit-basic/lib/orbit_app/module/registration.rb

117 lines
3.2 KiB
Ruby

module OrbitApp
module Module
module Registration
Version = "0.1"
module ClassMethods
@@registrations = []
def new( name ,&block)
@@registrations << DataSheet.new(name,&block)
end
def find_by_key(key)
@@registrations.each{|t|
return t if t.key == key
}
return nil
end
def all
return @@registrations
end
end
extend ClassMethods
def self.included( other )
other.extend( ClassMethods )
end
class DataSheet
attr_reader :name,:key,:base_path,:module_label
def initialize(name, &block)
@name = name
@key = name.underscore.singularize
@side_bar = nil
@front_end_app_pages = nil
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
setup_module_app
end
def setup_module_app
module_app = ModuleApp.first(conditions: {:key=>@key,:title=>name})
module_app = ModuleApp.new(:key=>@key,:title=>name) if module_app.nil?
begin
module_app.save(:validate=>false)
rescue
retry
end
end
def get_label_i18n
@module_label
end
def get_default_widget
if @widget_set.nil? # Init not defining widget
return {}
elsif @widget_set.get_default_widget.nil? # Init had defining widget,but say nothing about default_widget
return {}
else
@widget_set.get_default_widget.to_module_app_format
end
end
def get_enable_frontend
(@front_end_app_pages.nil? ? false : true)
end
def get_app_pages
@front_end_app_pages.nil? ? [] : @front_end_app_pages.to_module_app_format
end
def get_widgets
@widget_set.nil? ? {} : @widget_set.to_module_app_format
end
def get_categories
eval(@widget_set.categories_query) rescue nil
end
def get_tags
eval(@widget_set.tags_query) rescue nil
end
def front_end(&block)
@front_end_app_pages = FrontendUtility::AppPageSet.new(&block)
end
def side_bar(&block)
@side_bar = SideBarRegisition::SideBar.new(@name,@key,&block)
end
def personal_plugin(params)
# TODO 這裡要看是一些檔案是不是都有
Plugin::Registration.new_from_module_app(@name,@base_path,params)
end
def widgets(&block)
# @widgets = WidgetRegisition::WidgetSet.new(&block)
@widget_set = WidgetUtility::WidgetSet.new(&block)
# @widgets = widget_set.widgets
# @default_widget = widget_set.default_widget
end
%w{module_label category base_url version organization author intro update_info}.each do |field|
define_method(field){|var| instance_variable_set( "@" + field, var)}
end
end # of class DataSheet
end
end
end