Given an object, how to find public methods in included modules and the parent module?

D

danielbuus

Hey :)

I'm trying to extend a method for creating a site map for a Ruby on
Rails app. This question, I believe, is pretty much just Ruby though,
that's why I'm asking here.

Basically, what it does ATM is find all classes that end in
'_controller.rb' and require them,

require 'find'

# Require all controllers
Find.find(File.join(RAILS_ROOT, "app/controllers")) { |path|
require_dependency path if path =~ /_controller\.rb$/ }

Okay. So I have two issues, and therefore two questions for this:

a) The "app/controllers" path does not just contain files, it also
contains directories, each of which present a module, like "Manage::"
for the backend, and "Store::" for the frontend. Inside are class
files that share names, e.g. 'Store::Items' and 'Manage::Items'. Only
difference is the module they belong to.

b) In the backend classes, I include a module, 'include Backend::Crud'
which defines some shared methods on the backend classes.

My question for a) is, how can I distinguish between the different
classes, given that they belong to different modules. How can I ask an
object what module it belongs to?

And, b), how can I look up these included methods? Right now, I can
only find public methods that are in the class files themselves, not
those defined in included modules...

I'm a bit confused with all this, it's pretty low-level for me ;) So I
apologize if I'm asking very stupidly. If you have any ideas of how I
could do this, please let me know.

Thanks in advance,
Daniel
 
A

ara.t.howard

Hey :)

I'm trying to extend a method for creating a site map for a Ruby on
Rails app. This question, I believe, is pretty much just Ruby though,
that's why I'm asking here.

Basically, what it does ATM is find all classes that end in
'_controller.rb' and require them,

require 'find'

# Require all controllers
Find.find(File.join(RAILS_ROOT, "app/controllers")) { |path|
require_dependency path if path =~ /_controller\.rb$/ }

Okay. So I have two issues, and therefore two questions for this:

a) The "app/controllers" path does not just contain files, it also
contains directories, each of which present a module, like "Manage::"
for the backend, and "Store::" for the frontend. Inside are class
files that share names, e.g. 'Store::Items' and 'Manage::Items'. Only
difference is the module they belong to.

b) In the backend classes, I include a module, 'include Backend::Crud'
which defines some shared methods on the backend classes.

My question for a) is, how can I distinguish between the different
classes, given that they belong to different modules. How can I ask an
object what module it belongs to?

And, b), how can I look up these included methods? Right now, I can
only find public methods that are in the class files themselves, not
those defined in included modules...

I'm a bit confused with all this, it's pretty low-level for me ;) So I
apologize if I'm asking very stupidly. If you have any ideas of how I
could do this, please let me know.

Thanks in advance,
Daniel

can you start out by telling us __what__ you are trying to accomplish?

-a
 
D

danielbuus

can you start out by telling us __what__ you are trying to accomplish?

Yes :) I'm trying to basically "load in" a bunch of class files and
detect their public methods, including those defined in included
modules. Consider this file structure:

apps/
controllers/
store/
items_controller.rb -> class Store::ItemsController <<
ApplicationController
def index
# blah blah
end
end
manage/
items_controller.rb -> class Manage::ItemsController <<
ApplicationController
include Poly::BackendCrud
def show
# blah blah
end
end
lib/
poly.rb -> module Poly
module BackendCrud
def save
# blah blah
end
end
end

Okay. Given that file tree, I want to "examine" all controllers in app/
controllers/, so that I can produce something like this:
{ 'store' => {
'items' => [
'index'
]
},
'manage' => {
'items' => [
'show',
'save'
]
}
}

Do you know what I mean?

Cheers,
Daniel :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top