These are chat archives for dry-rb/chat

17th
Jul 2015
Piotr Solnica
@solnic
Jul 17 2015 11:49
hey @AMHOL I’m about to add dry stuff to my project at work :)
Andy Holland
@AMHOL
Jul 17 2015 11:57
Cool :smile: Is it container or config?
Piotr Solnica
@solnic
Jul 17 2015 12:11
@AMHOL it’s a rails app so just the container
Andy Holland
@AMHOL
Jul 17 2015 12:11
Ahh OK, cool
Piotr Solnica
@solnic
Jul 17 2015 12:11
I would like to finally organize dependency resolution somehow
need to figure out how to connect that with controllers in a non-messy way
Andy Holland
@AMHOL
Jul 17 2015 12:12
Yeah, that's something I think Laravel does nicely with the IoC container
Piotr Solnica
@solnic
Jul 17 2015 12:12
did you maybe do that already?
Andy Holland
@AMHOL
Jul 17 2015 12:12
Nah, it was pretty messy in the roda-playground repo
Thinking about it, it could be nice to set the container as an instance variable in the base controller, then add a macro method to define methods that resolve from the container?
Piotr Solnica
@solnic
Jul 17 2015 12:20
yeah that was my initial plan actually
I thought about sth like
class MehController < ApplicationController
  inject :foo_client, with: { some_args }, only: [:index, :other, :action]
end
Andy Holland
@AMHOL
Jul 17 2015 12:32
What would the with: some_args do?
Piotr Solnica
@solnic
Jul 17 2015 12:33
hmm NO IDEA lol
Andy Holland
@AMHOL
Jul 17 2015 12:34
Was the roda-action stuff with the bind_arguments, guess you was thinking of that?
Piotr Solnica
@solnic
Jul 17 2015 12:34
but the whole thing would just define a before_action block that would set up an ivar + a reader for it for specific actions and use resolve mechanism from the container
might be yeah :)
Andy Holland
@AMHOL
Jul 17 2015 12:43
Was thinking sth like:
class Container
  extend Dry::Container::Mixin

  register(:data_store) do
    ThreadSafe::Cache.new.tap do |ds|
      ds[:users] = ThreadSafe::Array.new
    end
  end
  register(:user_repository, -> { container.resolve(:data_store)[:users] })
end

module ContainerResolution
  def uses(*keys)
    keys.each do |key|
      define_method(key) do
        resolve(key)
      end
    end
  end
end

class BaseController
  extend ContainerResolution
  delegate :resolve, to: Container
end

class UsersController < BaseController
  uses :user_repository

  def show
    user_repository[user_id]
  end

  private

  def user_id
    params[:user_id]
  end
end
Piotr Solnica
@solnic
Jul 17 2015 12:44
what about memoization?
Andy Holland
@AMHOL
Jul 17 2015 12:45
Good point, could do:
      define_method(key) do
        instance_variable_get("@#{key}") || instance_variable_set("@#{key}", resolve(key))
      end
Or add a memoize option to uses and define the method based on that
Piotr Solnica
@solnic
Jul 17 2015 12:46
there’s memoizable gem
maybe could use that
Andy Holland
@AMHOL
Jul 17 2015 12:48
Yeah, could do, would still pass it as an option though, then just delegate it to the memoizable gem
module ContainerResolution
  def uses(*keys, **options)
    keys.each do |key|
      define_method(key) do
        resolve(key)
      end
      memoize(*keys) if options.fetch(:memoize, false)
    end
  end
end
Piotr Solnica
@solnic
Jul 17 2015 12:50
this looks sweet
Andy Holland
@AMHOL
Jul 17 2015 12:50
Yeah, got me all excited now lol
I wanna work on what you're working on :p
Piotr Solnica
@solnic
Jul 17 2015 12:51
it’s rails :P
but lots of ROM :)
Andy Holland
@AMHOL
Jul 17 2015 12:51
I work on Rails 5 days a week :laughing:
It's funny because when I talk to people about using IoC in Ruby, they seem to think it's not necessary/useful in Ruby because it doesn't have interfaces/typehinting, then begin to bang on about duck typing
Piotr Solnica
@solnic
Jul 17 2015 13:00
let them be
Andy Holland
@AMHOL
Jul 17 2015 13:00
Yep :p
Piotr Solnica
@solnic
Jul 17 2015 13:02
people are mixing concepts
lots of rubyists are “educated” by misguided posts like DHH’s stuff about DI or TDD etc
where he basically says things like “I don’t need DI because I can monkey-patch Time"
Andy Holland
@AMHOL
Jul 17 2015 13:04
:joy: fantastic
Why spend time designing a resilient API, when you can re-design a language
Piotr Solnica
@solnic
Jul 17 2015 14:54
@AMHOL ok, we’re using dry-container already :):
Andy Holland
@AMHOL
Jul 17 2015 14:55
@solnic cool, you using it like above or did you go with something different?
Piotr Solnica
@solnic
Jul 17 2015 15:07
I created a custom container object, injected it to my app’s object. we have a global AppNamespace.app singleton, I initialize it in an initializer (dooh) and register stuff in the container there, then in app’s controller I did the uses macro
so, the app object has the container and rails app configuration too, so there’s access to both config and container
Andy Holland
@AMHOL
Jul 17 2015 15:08
Nice
Andy Holland
@AMHOL
Jul 17 2015 18:31
So you don't register anything with the container during the request lifecycle?
Piotr Solnica
@solnic
Jul 17 2015 18:38
nope
Andy Holland
@AMHOL
Jul 17 2015 18:40
Probably better that way :smile:
lol you see Aaron Patterson on Twitter?
Piotr Solnica
@solnic
Jul 17 2015 18:45
heh
Andy Holland
@AMHOL
Jul 17 2015 18:46
Did you see the commit on rom-rb.org this morning? Just committed to master as it seemed like an obvious one, hope that's ok?
Piotr Solnica
@solnic
Jul 17 2015 18:46
@AMHOL yes, thanks for that, I already deployed
I also fixed migration examples
Andy Holland
@AMHOL
Jul 17 2015 18:47
Cool, and no problem