0

私はいくつかのモジュールを持っています、Capybara::DSL例えばRSpec::Matchers、、、、。RouterCommon

コード内のほぼどこからでも、これらのモジュールのメソッドを使用できるようにしたいと思います。現在、私は次のことを試みました。

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

次に、それを次のように使用したいと思います。

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

モジュールまたはクラスのいずれかに含まれている場合に、インスタンスメソッドとクラスメソッドの両方としてこれらすべてのメソッドにアクセスできるようにするメソッドを知っていますか?

4

1 に答える 1

-1

モジュールを含めたいクラスincludeとの両方を使用するのはどうですか?extend

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
于 2013-03-26T10:16:59.157 に答える