Module methods with access to controller variables

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs