forked from saurabh/orbit4-5
81 lines
2.1 KiB
Ruby
81 lines
2.1 KiB
Ruby
#This is a core class of Orbit Kernel. This class will register a module and set the properties defined in the module's initializer
|
|
#file.
|
|
module OrbitApp
|
|
module Widget
|
|
module Registration
|
|
module ClassMethods
|
|
|
|
#initiate a blank array for registration
|
|
cattr_accessor :registrations
|
|
@@registrations = []
|
|
|
|
def new(name,&block)
|
|
@@registrations << RegisteredWidget.new(name,&block)
|
|
end
|
|
|
|
def find_by_key(key)
|
|
@@registrations.each{|t|
|
|
return t if t.key.eql?(key)
|
|
}
|
|
return nil
|
|
end
|
|
|
|
def all
|
|
return @@registrations
|
|
end
|
|
end
|
|
|
|
extend ClassMethods
|
|
def self.included(other)
|
|
other.extend( ClassMethods )
|
|
end
|
|
|
|
class RegisteredWidget
|
|
attr_reader :name,:key,:widget_label, :widget_methods, :widget_settings
|
|
|
|
def initialize(name,&block)
|
|
@name = name
|
|
@key = @name.underscore.singularize
|
|
@widget_label = @name
|
|
@widget_methods = []
|
|
@widget_settings = {}
|
|
|
|
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
|
|
setup_module_app
|
|
end
|
|
|
|
def get_orbit_widget
|
|
OrbitWidget.find_by(key: @key, title: name) rescue nil
|
|
end
|
|
|
|
def setup_module_app
|
|
orbit_widget = get_orbit_widget
|
|
orbit_widget = OrbitWidget.new(:key=>@key,:title=>name) if orbit_widget.nil?
|
|
orbit_widget.refetch_setting!(self)
|
|
orbit_widget.save(:validate=>false)
|
|
end
|
|
|
|
%w{widget_label version organization author intro update_info}.each do |field|
|
|
define_method(field){|var| instance_variable_set( "@" + field, var)}
|
|
end
|
|
|
|
def widget_methods(widgets)
|
|
@widget_methods = widgets
|
|
end
|
|
|
|
def get_widget_methods
|
|
@widget_methods
|
|
end
|
|
|
|
def widget_settings(settings)
|
|
@widget_settings = settings.first
|
|
end
|
|
|
|
def get_widget_settings
|
|
@widget_settings
|
|
end
|
|
|
|
end
|
|
end
|
|
end
|
|
end |