# Module methods with access to controller variables

**URL:** <https://www.ruby-forum.com/t/module-methods-with-access-to-controller-variables/122224>\
**Category:** Rails\
**Created:** [December 5, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/module-methods-with-access-to-controller-variables/122224 "2007-12-05T18:39:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tristan\_Dunn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e8cbc/32.png) [@Tristan\_Dunn](https://www.ruby-forum.com/u/Tristan_Dunn)\
**Post date:** [December 5, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/module-methods-with-access-to-controller-variables/122224/1 "2007-12-05T18:39:53Z")

</div>

My subject is probably not the best description, so here is an example  
of what I am trying to do.

class ExampleController \< ApplicationController  
include EventManager

def action  
@name = ‘John’  
event(:event, :context) do  
@name \<\< ’ Doe’  
end  
end  
end

module EventManager  
def event(\*args)  
# Retrieve the module based on the parameters.  
# Call EventManager::Event::Context.before if it exists with  
access to the variables from where event was called.  
yield if block\_given?  
# Call EventManager::Event::Context.after if it exists with access  
to the variables from where event was called.  
end

module Event  
module Context  
def before  
puts “Hi John” if @name == ‘John’  
end

```
  def after
    puts "Who?" if @name != 'John'
  end
end

```

end  
end

Basically I want to be able to store methods in nested modules for  
separation, but be able to call them dynamically within controller  
actions via the event method.

My current solutions involves module\_function for all the before and  
after methods and retrieving controller information by passing the  
controller to each action and using send and instance\_variable\_get/set  
which is less than ideal in my opinion.

Any suggestions, or hints?
