These are chat archives for dry-rb/chat

27th
May 2017
Stergio
@stergiom
May 27 2017 00:14

@timriley, I ended up recursing till everything resolved (or something could not)..

class Registration
  extend Dry::Monads::Try::Mixin

  def self.auto_resolve(container, namespace, class_list = nil, precedent = [])
    class_list ||= namespace.constants.map(&:to_s)

    cycle = class_list.select do |n|
      Try(Dry::Container::Error) { container.register(n.snake_case, namespace.const_get(n).new) }.failure?
    end

    return if cycle.empty?
    raise(Dry::Container::Error, "Unresolvable: #{cycle}") if (cycle <=> precedent).zero?

    send(__method__, container, namespace, cycle, class_list)
  end
end

Registration.auto_resolve(ServiceContainer, Services)
# -or-
Registration.auto_resolve(ServiceContainer, Services, Services.constants.select { .. })

might not be the optimal approach for dry-container but its doing okay for me right now, dynamic resolution for singletons would be a nice plus.

Michael Johnston
@lastobelus
May 27 2017 00:22
in dry-validation, does anyone know of a way to make a high level rule use a predicate that depends on more than two values?
ie, this is fine:
```ruby
rule(whatever: [:foo, :bar, :quaz]) do | foo, bar, quaz|
foo.my_arity_2_predicate?(value(:bar))
end
rule(whatever: [:foo, :bar, :quaz]) do | foo, bar, quaz|
  foo.my_arity_2_predicate?(value(:bar))
end
but I can't find any way to do this:
rule(whatever: [:foo, :bar, :quaz]) do | foo, bar, quaz| foo.my_arity_3_predicate?(value(:bar), value(:quaz)) # predicate arity is invalid (ArgumentError) end
gah
rule(whatever: [:foo, :bar, :quaz]) do | foo, bar, quaz|
  foo.my_arity_3_predicate?(value(:bar), value(:quaz)) # predicate arity is invalid (ArgumentError)
end
Tim Riley
@timriley
May 27 2017 11:55
@flash-gordon Thanks for taking care of that -auto_inject release, sorry I didn’t get to it sooner.
Nikita Shilnikov
@flash-gordon
May 27 2017 11:56
@timriley sure, no worries man, btw I did that when I injected deps in my repos
Tim Riley
@timriley
May 27 2017 11:57
oh? Mind if I see an example of what you’re doing?
Nikita Shilnikov
@flash-gordon
May 27 2017 12:01
@timriley yeah, I added include AutoInject['time_machine'] where time_machine is a project-wide clock, then I call time_machine.now inside the repo rather than Time.now
Tim Riley
@timriley
May 27 2017 12:02
Oh interesting. This is a rom-repo? How are you combining the time_machine dep with the rom dep that the repo needs?
Nikita Shilnikov
@flash-gordon
May 27 2017 12:04
it just works :) I have a line like MyRepo.new(rom_container) in my app container, I still didn't bother with any smart registration so it works OOTB
Tim Riley
@timriley
May 27 2017 12:05
Ah, I see!
I was trying to think about how that might work with the way we have repos configired atm:
module MyApp
  class Repository < ROM::Repository::Root
    include MyApp::Import.args["persistence.rom"]
  end
end
I guess we’d just confinue to use an args based constructor and throw the time_machine on that list
Nikita Shilnikov
@flash-gordon
May 27 2017 12:08
for me it's not the case because I use same repos with different rom containers
Tim Riley
@timriley
May 27 2017 12:09
oh!
Is this so you can work with different data sources or something?
Nikita Shilnikov
@flash-gordon
May 27 2017 12:12
nope, two different containers with different connections, one has access to all data in the database and the other one is restricted with the client identifier, I called them public/private and use private exclusively for authentication
so technically it's the same database
Tim Riley
@timriley
May 27 2017 12:15
Ah, that’s clever.