[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Proper way to include module methods with dynamic code?

Web Manager

3/6/2009 3:26:00 PM

Hi,

(This is in a Rails context, but I believe is much more of a Ruby
question so is posted here.)

I've got an index method with some dynamic bits I'd like to place into a
module and include in several controllers:

module MyApp::ListsControllerIndex
def index
self.instance_variable_set( "@#{@models_str}", @klass.find(:all) )

respond_to do |format|
format.html do
render 'shared/lists_index'
end
end
end # End index
end # End module

(The instance variables @models_str and @klass are set by a
before_filter in the controller.)

When I try to include this module into controllers via any combination
of include or extend, I either get:

1) a nil.find error as Ruby seems to try and execute/resolve @klass in
@klass.find when included rather than after the filters have run (seen
with include)
2) the included method is not found (seen with extend)

What I'd like is the method to be added to my controller class and be
sitting ready and waiting to evaluate the dynamic bits when called. (Or
said another way, for the included method to work successfully the same
way it does when it's defined directly in the controller or a superclass
of the controller.)

I've read through include/extend docs but clearly need some additional
help. Any is much appreciated and thanks for your time!

Cheers.
--
Posted via http://www.ruby-....

2 Answers

Nanfang Zhao

3/6/2009 3:55:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Methods in module will become the instance methods of the class which
includes this module.
make sure your before_filter is running properly, and @klass is defined.


On Fri, Mar 6, 2009 at 9:25 AM, Web Manager <webmanager@aibs.org> wrote:

> Hi,
>
> (This is in a Rails context, but I believe is much more of a Ruby
> question so is posted here.)
>
> I've got an index method with some dynamic bits I'd like to place into a
> module and include in several controllers:
>
> module MyApp::ListsControllerIndex
> def index
> self.instance_variable_set( "@#{@models_str}", @klass.find(:all) )
>
> respond_to do |format|
> format.html do
> render 'shared/lists_index'
> end
> end
> end # End index
> end # End module
>
> (The instance variables @models_str and @klass are set by a
> before_filter in the controller.)
>
> When I try to include this module into controllers via any combination
> of include or extend, I either get:
>
> 1) a nil.find error as Ruby seems to try and execute/resolve @klass in
> @klass.find when included rather than after the filters have run (seen
> with include)
> 2) the included method is not found (seen with extend)
>
> What I'd like is the method to be added to my controller class and be
> sitting ready and waiting to evaluate the dynamic bits when called. (Or
> said another way, for the included method to work successfully the same
> way it does when it's defined directly in the controller or a superclass
> of the controller.)
>
> I've read through include/extend docs but clearly need some additional
> help. Any is much appreciated and thanks for your time!
>
> Cheers.
> --
> Posted via http://www.ruby-....
>
>

Web Manager

3/6/2009 6:38:00 PM

0

Hi Nanfang,

Thanks for your response... you were indeed correct sir, it was my
oversight re: the before filter.

All best!
--
Posted via http://www.ruby-....