Using web-container in a gem

I want to make a gem that encapsulates a web app, similar to ‘sidekiq-web’ for example, than I can later mount inside a rack based app ilke rails.

Unfortunately I hit a roadblock when the container tries to resolve dependencies. Here is a an example container:

module Api
  class Container < Dry::Web::Container
    require root.join('system/notification/container')
    import Notification::Container

    configure do |config|
      config.name = :api
      config.default_namespace = 'api'

      config.root = Pathname(__FILE__).join('../..').realpath.dirname.freeze

      config.auto_register = %w(
        lib
      )
    end

    load_paths! 'lib', 'component'
  end
end

The root will actually point inside the projects that has the gem as dependency making it impossible for the autoload to work.

Any way I can get this working?

That’s a very interesting usecase, I believe we should be able to support it. Could you try patching container class with the following:

require 'dry/system/container'

class Dry::System::Container
  def self.root
    @root ||= config.root.is_a?(Pathname) ? config.root : config.root.call
  end
end

And then in your setup just configure the root as a proc:

module Api
  class Container < Dry::Web::Container
    require root.join('system/notification/container')
    import Notification::Container

    configure do |config|
      config.name = :api
      config.default_namespace = 'api'

      config.root = -> {
        # do whatever you need here to resolve the path
      }

      config.auto_register = %w(
        lib
      )
    end

    load_paths! 'lib', 'component'
  end
end

If that can work for you, I’d be happy to accept a PR which implements this feature.