I'm writing a simple callback for a gem I'm working on. Please note that due to certain architectural constraints, I can't use the ActiveSupport define_callbacks
method for this specific case.
Right now, I have something like this:
def self.after_data_transcoding(&block)
define_method :_after_data_transcoding_callback { block.call }
end
So an use case is similar to this:
class MyClass
after_data_transcoding do
end
end
The actual call is done by instance.send(:_after_data_transcoding_callback)
.
The code so far works great. I would have liked to go one step further and be able access the response as a block argument:
class MyClass
after_data_transcoding do |response|
# Do something with the response
end
end
However, I haven't had much success. Any thoughts on how I should proceed?
Turns out this was easier than I thought.
define_method(:_after_data_transcoding_callback) { |response|
block.call(response)
}
instance.send(:_after_data_transcoding_callback, response)