Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of initializer blocks could be more streamlined #19

Open
KevinBrowne opened this issue Feb 21, 2024 · 0 comments
Open

Use of initializer blocks could be more streamlined #19

KevinBrowne opened this issue Feb 21, 2024 · 0 comments

Comments

@KevinBrowne
Copy link
Member

KevinBrowne commented Feb 21, 2024

When registering a dependency with an initializer block, Sinject::Container#register requires the class to be provided as a parameter and, essentially, to be provided in the block. This introduces a bit of tedious boilerplate, like this:

require 'sinject'
container = Sinject::Container.new(false)

module Some
  module Deeply
    module Nested
      module NameSpace
        class Thing
          def initialize(whatever)
          # etc.
        end
      end
    end
  end
end

container.register(key: :foo,
                   class: Some::Deeply::Nested::NameSpace::Thing) do
  Some::Deeply::Nested::NameSpace::Thing.new(something)
end

Nine times out of ten, the type you want to instantiate in the block is the exact same type you specified as the class argument. It would be a nice convenience if that class could be passed to the block:

container.register(key: :foo,
                   class: Some::Deeply::Nested::NameSpace::Thing) { |klass| klass.new(something) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant