Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Andy Holland
@AMHOL
Is it something for jRuby?
Piotr Solnica
@solnic
Haha
Rails app preloader
Andy Holland
@AMHOL
Ahh OK, does it cause problems with dry-container or something?
Piotr Solnica
@solnic
With my code and the way I instantiate things
I build an app instance in the initialier but one of the internal deps get nullified when spring restarts
As in one of the things that container resolves becomes nil
Will be looking into it tomorrow
Andy Holland
@AMHOL
Ahh OK, seems strange that it nullifies it
Cool, you've got commit access if you need to change anything
Piotr Solnica
@solnic
Adding dry-container.rb file would be nice
It is a common convention that you can require a gem using its name
Andy Holland
@AMHOL
:+1: cool, I'll add it and release
@solnic can you PM me your email, I'll add you as an owner so you can push to rubygems
Piotr Solnica
@solnic
piotr dot solnica at gmail
Andy Holland
@AMHOL
:+1: cheers
Added
Andy Holland
@AMHOL
released with dry-container and dry-configurable files
Oops, forgot the rubocop disable
Andy Holland
@AMHOL
@solnic not sure how far you got or what code you ended up with for the container stuff, but thinking sth like this would be better:
class Container
  extend Dry::Container::Mixin

  register('rom') do
    ROM.env
  end

  register('repositories.user') do
    resolve('rom').relation(:users)
  end
end

module ContainerResolution
  def self.extended(klass)
    klass.delegate :resolve, to: Container
  end

  def use(key, **options)
    method = options.fetch(:as, key)

    define_method(method) do
      resolve(key)
    end

    memoize(*keys) if options.fetch(:memoize, false)
  end
end

class UsersController
  extend ContainerResolution

  use 'repositories.user', as: :user_repository

  def index
    user_repository.all.to_a
  end
end
Just with the use 'sth', as: :sth_els, I like the namespaces with the string keys
Piotr Solnica
@solnic
oh that’s a neat idea
I already thought that this will grow fast so I dunno how to split it up
namespacing like that sounds really simple!
Andy Holland
@AMHOL
Yeah, not sure it adds much to the code, but it's definitely easier for humans to understand :)
Piotr Solnica
@solnic
that’s really cool, I think, we’ll see how it works :)
btw I did a sweet little thing for auto-injecting with Concord + Dry::Container
it looks like that, more or less:
class Foo
  extend AutoInject(:baz, :bar)

  def call
    bar.call(baz.call)
  end
end
which basically creates a module and sets up constructor with deps injected from a known container
and returns it, so Foo is extended with it
Andy Holland
@AMHOL
Cool, constructor injection is much better
What does AutoInject look like?
Piotr Solnica
@solnic
def AutoInject(*names)
  Module.new do |mod|
    mod.class_variable_set('@@names', names)

    def self.extended(klass)
      names = class_variable_get('@@names')

      klass.class_eval do
        include Concord.new(*names)

        def self.new(*args)
          names = ancestors.detect { |a| a.is_a?(Concord) }.names
          deps = names.map.with_index { |name, i| args[i] || SB.app.container.resolve(name) }
          super(*deps)
        end
      end
    end
  end
end
messy but works
I wrote it yesterday in anger
Andy Holland
@AMHOL
lol
Piotr Solnica
@solnic
:D
Andy Holland
@AMHOL
Nice
Piotr Solnica
@solnic
it removes helluva code from class definitions
Andy Holland
@AMHOL
You have stuff registered with call: false then?
Piotr Solnica
@solnic
and you can still easily track down what the deps are
I register them with blocks
no options
works
Andy Holland
@AMHOL
Ahh OK
Piotr Solnica
@solnic
container.register(:persistence) { Persistence.new(resolve(‘rom’)) }
things like that
Andy Holland
@AMHOL
Just with:
class Foo
  extend AutoInject(:baz, :bar)

  def call
    bar.call(baz.call)
  end
end
And the default resolver, it would call on resolution