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

Discussion in 'Ruby' started by dblock, Jul 22, 2011.

  1. dblock

    dblock Guest

    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?
     
    dblock, Jul 22, 2011
    #1
    1. Advertising

  2. dblock

    dblock Guest

    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
     
    dblock, Jul 22, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VS_NET_DEV
    Replies:
    2
    Views:
    3,817
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    713
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Adam Anderson
    Replies:
    6
    Views:
    139
    Gary Wright
    Nov 7, 2007
  4. dutone
    Replies:
    8
    Views:
    112
    dutone
    Jul 2, 2004
  5. Phlip
    Replies:
    13
    Views:
    192
    Phlip
    Dec 17, 2006
Loading...

Share This Page