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

Add an ability to import two containers in the same class/module #10

Open
woarewe opened this issue Feb 17, 2023 · 0 comments
Open

Add an ability to import two containers in the same class/module #10

woarewe opened this issue Feb 17, 2023 · 0 comments

Comments

@woarewe
Copy link
Owner

woarewe commented Feb 17, 2023

Currently, if we try to do something like this:

A = F::Container.new.tap do |container|
  F::Container::Define.build(container: container).instance_exec do
    namespace :math do
      f(:value) { "A" }
    end
  end
end

B = F::Container.new.tap do |container|
  F::Container::Define.build(container: container).instance_exec do
    namespace :math do
      f(:value) { "B" }
    end
  end
end

class Service
  include A.import
  include B.import

  def call
    f("math.value")
  end
end

puts Service.new.() # => 'B'

Basically, if you try to import several containers the last one overrides all the previous ones.
But instead, we should make it work according to the following specification

Importing a container

We need to add a mechanism to store the imported containers in a box with assigned labels.
The first imported container gets the label main. Basically, the target class or a module should have a storage, like a hash, with labels as keys and with containers as values.

For example:

class Service
  include A.import # this line should store { main: A }
  
  def call
    f('math.value') # => should work
    f('main.math.value') # should also work
  end
end

If one container is imported

  • The behavior stays the same

** If several containers are imported

  • An attempt to import a second module without an explicit alias should raise an error
  class Service
  include A.import # this line should store { main: A }
  include B.import # this line should fail
  include B.import.as(:second) # ok will work
end
  • The function f should look through the containers from top to bottom if there is no top namespace provided.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant