These are chat archives for dry-rb/chat

3rd
Apr 2017
choallin
@choallin
Apr 03 2017 02:29
Hi folks! I'm starting to use dry-system. Unfortunately, I'm getting a little bit confused about the finalize! method. Where/When do I have to call it so that all my dependencies are availiable?
Also, Is there a way to give my dependency some initial values for creating the object? I.e. how can I use this class as a dependency:
class SomeClass
  def initialize(arg1, arg2)
    @arg1 = arg1
    @arg2 = arg2
  end
end
Tim Riley
@timriley
Apr 03 2017 03:45

@choallin Typically you want to call finalize! as part of the “boot” process for your app (i.e. something that happens just once when the application starts/boots).

For providing dependencies to your SomeClass example, you might like to look into dry-system’s auto-injection feature: http://dry-rb.org/gems/dry-system/auto-import/

choallin
@choallin
Apr 03 2017 03:59
@timriley Thanks for your support. Now I know where to put the finalize call. :smile:
For the 2nd part: I think you misunderstood me. I know how to configure dependencies for a class, but I don't know how to pass in arg1 and arg2 INTO my dependency. Like this:
class OtherClass
  Injector['someclass'] # how can I pass the args to SomeClass?
end
Andy Holland
@AMHOL
Apr 03 2017 11:54
@choallin there are a few different ways you can do it, you either need to use the Injector in SomeClass to inject dependencies from your container there, or you can make SomeClass a bootable dependency, alternatively, you can place it outside of your autoloading directory structure and register it manually.
choallin
@choallin
Apr 03 2017 12:36
@AMHOL Hmm.. The problem here is, that I cannot say what the arguments could be unless I instantiate it and use the methods from the instance. i.e. the args are depending on the country from my user. Another option I see is, that I pass this to the call method ...
Andy Holland
@AMHOL
Apr 03 2017 12:38
@choallin if the arguments can only be determined at run-time, you're better off either instantiating the class directly or registering it manually with:
MyContainer.register('some_class') do |arg_1, arg_2|
  SomeClass.new(arg_1, arg_2)
end
Then it will be resolved as a proc that takes two arguments
i.e. MyContainer['some_class'].call('arg_1', 'arg_2')
choallin
@choallin
Apr 03 2017 12:41
Ok, thanks for your hints! :smile: I think I will add the arguments to the call method of my class. Since this is the only public interface it will actually increase the reusability of an instance I think...
Again, thanks!
Andy Holland
@AMHOL
Apr 03 2017 12:55
:+1:
Pablo Herrero
@pabloh
Apr 03 2017 23:08
is possible to use dry-v, to fill errors on a single attribute, given some conditions on that and other attributes?