rom-rb / rom

Data mapping and persistence toolkit for Ruby
https://rom-rb.org
MIT License
2.08k stars 161 forks source link

Support passing container to Repository as either param or option #504

Closed timriley closed 5 years ago

timriley commented 5 years ago

Being able to pass container as an option means Repository can work with an all-options constructor signature, which makes it easier to inject additional app-specific dependencies to subclasses using tools like dry-auto_inject (with its standard kwargs strategy).

This change is backwards compatible because the standard param continues to be respected.

So now you can initialize a repo in both these ways:

UserRepo.new(rom)
UserRepo.new(container: rom)

And here's an example of how it'd with with dry-auto_inject:

module MyApp
  class Repo < ROM::Repository
    include Import[container: "persistence.rom"]
  end

  class UserRepo < Repo
    include Import["another_dep"]

    # container is injected via superclass
    # can also work with `another_dep` inside instance methods here
  end
end

@solnic @flash-gordon How's this look to you?

timriley commented 5 years ago

I'm not sure what our policy with codeclimate issues is here, but I'd be happy to shorten those yardoc lines 🤷‍♂️

solnic commented 5 years ago

I'm not sure what our policy with codeclimate issues is here

It's to use common sense 😄