Created
January 13, 2016 16:57
-
-
Save litvil/3192139640f4312f15db to your computer and use it in GitHub Desktop.
ControllerRegistrator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ControllerRegistrator | |
def get_controller_class(name) | |
if self.class.controller_classes[name].is_a?(Class) | |
return self.class.controller_classes[name] | |
end | |
self.class.controller_classes[name].call(self) | |
end | |
def controllers | |
@controllers ||= {} | |
end | |
def controllers=(controllers) | |
@controllers = controllers | |
end | |
def get_common_methods(manifest_key, method_name) | |
method_name = method_name.to_sym | |
manifest_key = manifest_key.to_sym | |
self.class.controllers_common_methods[manifest_key] ||= {} | |
common_methods = self.class.controllers_common_methods[manifest_key][method_name] | |
return common_methods if common_methods.present? | |
common_methods = [] | |
self.class.controller_classes.keys.each{|controller_name| | |
controller_class = get_controller_class(controller_name) | |
manifest = controller_class::CONTROLLER_MANIFEST[manifest_key] | |
if manifest.present? && manifest[method_name].present? | |
call_method = manifest[method_name] | |
call_method = {:name => call_method, :priority => 0} unless call_method.is_a?(Hash) | |
call_method[:controller_name] = controller_name | |
common_methods << call_method | |
end | |
} | |
common_methods.sort_by!{|c| -(c[:priority] || 0)} | |
self.class.controllers_common_methods[manifest_key][method_name] = common_methods | |
end | |
def call_controllers_method(method_name, args = []) | |
res = {} | |
get_common_methods(:common_methods, method_name).each{ |call_method| | |
next if call_method[:need_init] && !controllers[call_method[:controller_name]] | |
res[call_method[:controller_name]] = self.send(call_method[:controller_name]).send(call_method[:name], *args) | |
} | |
res | |
end | |
def call_controllers_static_method(method_name, args = []) | |
res = {} | |
get_common_methods(:common_static_methods, method_name).each{ |call_method| | |
controller_class = get_controller_class(call_method[:controller_name]) | |
res[call_method[:controller_name]] = controller_class.send(call_method[:name], *args) | |
} | |
res | |
end | |
def perform_controller_command(data) | |
return false unless data[:command].present? | |
res = {} | |
command = data[:command].to_sym | |
if self.class.controllers_commands[command].present? | |
self.class.controllers_commands[command].each{ |controller_name, call_method| | |
res[controller_name] = self.send(controller_name).send(call_method, data) | |
} | |
else | |
self.class.controller_classes.keys.each{|controller_name| | |
controller_class = get_controller_class(controller_name) | |
self.controllers[controller_name] ||= controller_class.new(self) if controller_class < ProxyController | |
if controller_class::CONTROLLER_MANIFEST[:commands].present? && controller_class::CONTROLLER_MANIFEST[:commands][command].present? | |
call_method = controller_class::CONTROLLER_MANIFEST[:commands][command] | |
(self.class.controllers_commands[command]||={})[controller_name] = call_method | |
res[controller_name] = self.send(controller_name).send(call_method, data) | |
end | |
} | |
end | |
res | |
end | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def controllers_common_methods | |
@controllers_common_methods ||= {} | |
end | |
def controllers_commands | |
@controllers_commands ||= {} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment