I have an extension to ActiveRecord's class ActionController:
module MyExtension
def my_method
"default implementation"
end
end
ActionController::Base.class_eval { include MyExtension }
All my controllers inherit from ApplicationController
, which inherits from ActionController::Base
. I want to add to and override the methods in MyExtension
in the inherited class
class MyController < ApplicationController
module MyExtension
def my_method
"overridden implementation"
end
def my_new_method
"added method implementation"
end
end
end
I then want to look up the methods defined in MyExtension for a specific Controller class dynamically
controller = Kernel.const_get("MyController")
methods_in_my_extension = Kernel.const_get("MyController::MyExtension").public_instance_methods
Obviously, this does not work, since there are no MyController::MyExtension
constant defined.
In a more general sense, what I want to achieve is:
Group methods to implement default behavior for classes inheriting from
ActionController::Base
Be able to override and add to those methods in the class definition
Dynamically get a list of the grouped methods for a specific class
Edit: In answer to Deefour about why I want to do this:
I am creating a CLI menu to wrap around the controllers. It takes input from the user to call controller methods. The menu collects the methods directly from the controllers and thereby defines valid commands/input.
What I want is to define a new level of encapsulation, a new access specifier, a subset of the public methods, which is accessible to the CLI menu.
I could achieve this by just creating a prefix for all menu-available methods, but I am also trying to get a grip on the inner workings on ruby.