0
votes

I have several modules, say Capybara::DSL, RSpec::Matchers, Router, Common.

I'd want to be able to use methods from those modules nearly from anywhere in my code. Currently I attempted to do:

module Helper
  # from http://stackoverflow.com/a/4663029/841064
  module ClassMethods
    include Capybara::DSL
    include RSpec::Matchers
    include Router
    include Common
  end

  include Capybara::DSL
  include RSpec::Matchers
  include Router
  include Common

  extend ClassMethods

  def self.included(other)
    other.extend(ClassMethods)
  end
end

Then I'd want to use it as:

module A
  include Helper

  class << self
    # all methods from 4 modules are available in all methods here
  end

  # all methods from 4 modules are available in all methods here
end


class B
  include Helper

  class << self
    # all methods from 4 modules are available in all methods here
  end

  # all methods from 4 modules are available in all methods here. But they aren't available here
end

Do you know a method to get all those methods accessible as both instance and class methods when they are included to either module or class?

1

1 Answers

-1
votes

How about using both include and extend in the classes where you want to include the modules?

module Helper
  include Capybara::DSL
  include RSpec::Matchers
  include Router
  include Common
end

module A
  # Gives methods on instance
  include Helper

  # Gives methods on class
  extend Helper

  #...
end