How can I evaluate a module with another module's context?

D

dblock

I am trying to extend Grape (https://github.com/intridea/grape) to
support a more modular approach.

Grape defines the following.

module Grape
class API
class << self
def get(*paths, &block) ...
end
end
end

To use this one creates a class that derives from Grape::API.

class MyAPI < Grape::API
get "path" do ...
end

What I'd like to do is inject another definition into the MyAPI class
from, say, MyAPIMethods. So the latter would be declared like this.

module MyAPIMethods
get "someotherpath" do ...
end

Then I could write, for example:

class MyAPI < Grape::API
get "path" do ...
module MyAPIMethods
end

I'd like all the declarations of MyAPIMethods to evaluate within
MyAPI, so that it sets up routes accordingly. I can define module in
Grape::API to do anything. For example

def module(module_ref)
# what do I do here?
end

How do I evaluate a module in the context of MyAPI? Or is there a
better way to my madness?
 
D

dblock

I found a relatively simple way of accomplishing this.

module MyAPI
def self.included(parent)
parent.get '/ping' do
'pong'
end
end
end

Is there a better way? I want to write


module MyAPI
get '/ping' do
'pong'
end
end
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top