dry-rb / dry-auto_inject

Container-agnostic constructor injection mixin

Home Page:https://dry-rb.org/gems/dry-auto_inject/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How does `include Dry::AutoInject` works underneath

laertispappas opened this issue · comments

Hi guys,

I was playing around with this library and the related ones and I came up with this line of code: https://github.com/dry-rb/dry-auto_inject/blob/master/lib/dry/auto_inject.rb#L44

Can someone please explain me how this is possible? Since AutoInject::Builder is a class and not a module.

Thank you in advance.

It is a class but at the end it returns an instance of one of the strategy classes which are module instances. This is based on the module builder pattern that you can read about here: https://dejimata.com/2017/5/20/the-ruby-module-builder-pattern

Thanks @solnic for your feedback. I know the module builder pattern. My concern is that AutoInject::Builder is a BasicObject instance. How is it possible to be included in a class?

For instance:

class Some
  include Dry::AutoInject::Builder.new(Dry::Container.new)
end

# => TypeError (wrong argument type Dry::AutoInject::Builder (expected Module))

@laertispappas it's not Builder included, it's what Builder#[] returns, that is a module

@flash-gordon missed it. It is sends #[] message (I had the function call in my mind). Thanks a lot!

Glad you've figured it out. In the future, please ask questions on the forum, you can find the link in CONTRIBUTING.md (along with other instructions).

Sure I will @flash-gordon thank you.