untitled
untitled

Reputation: 1335

Rails: Override and also use existing gem module method

I've a gem with a helper module. eg

Gem module

module Hotel
 module MenuItem
  def menu(session)
    x = DEFAULT_FOOD_MENU[session]
  end

  def print_menu(menu)
    #printing menu
  end
end

custom class

module SizzSuzzHotel
 module MenuItem
   include Hotel::MenuItem
  def menu(session)
    # I want to use the default menu item and also specific menu related to this hotel! . 
  end
end

module SizzSuzzHotel
 class order
  include MenuItem
    def order(session)
       menu_item = menu(session)  
       print(menu_item)
    end


  end
end

Here I want to override the menu and I want to use the existing print_menu! How can I achieve this? use the gem module method and also add few more stuffs into it?

Upvotes: 1

Views: 1065

Answers (1)

Piotr Kruczek
Piotr Kruczek

Reputation: 2390

If you include the MenuItem module, both methods will be available, so you can easily redefine one of them.

include MenuItem

def menu(session)
  ... here you write you custom menu method behaviour
end

def order(session)
  menu_item = menu(session) # => above menu method will be called
  print(menu_item) # => print method from MenuItem module will be called
end

Upvotes: 2

Related Questions