These are chat archives for dry-rb/chat

17th
Aug 2015
Piotr Solnica
@solnic
Aug 17 2015 14:34
@AMHOL our container has ~150LOC; any ideas how to split it into smaller chunks?
Andy Holland
@AMHOL
Aug 17 2015 14:35
You mean registering stuff with it?
Piotr Solnica
@solnic
Aug 17 2015 14:35
yes
I’m loving the idea you gave me with dot notation btw
Andy Holland
@AMHOL
Aug 17 2015 14:35
Yeah, that's what I was thinking
Piotr Solnica
@solnic
Aug 17 2015 14:35
helped to categorize things nicely
Andy Holland
@AMHOL
Aug 17 2015 14:35
Could split it into namespaces
Piotr Solnica
@solnic
Aug 17 2015 14:36
yeah some sugar could be provided on the dsl side I think
Andy Holland
@AMHOL
Aug 17 2015 14:36
You doing it in an initializer?
Piotr Solnica
@solnic
Aug 17 2015 14:36
yeah
not sure where to put it
I tried separating it into smaller files but then rails loads it in an order that doesn’t work :)
Andy Holland
@AMHOL
Aug 17 2015 14:38
Nice, sth like:
container.namespace 'my_namespace' do |namespace|
  namespace.register('sth', -> {})
end
Piotr Solnica
@solnic
Aug 17 2015 14:38
yeah
I have one configure block exposed by the app singleton where I yield the container
so I have only one chance to register stuff because then it freezes everything
but that part I can change
Andy Holland
@AMHOL
Aug 17 2015 14:40
WDYT of:
container.register({
  item_1: -> {},
  item_2: -> {},
  # ...
})
Same with namespace
As an alternative method
Piotr Solnica
@solnic
Aug 17 2015 14:42
I wouldn’t use it
Andy Holland
@AMHOL
Aug 17 2015 14:42
:p
Piotr Solnica
@solnic
Aug 17 2015 14:42
it would look very cluttered
do...end is much cleaner, easier to parse with eyes you know :)
Andy Holland
@AMHOL
Aug 17 2015 14:44
Yeah, was just thinking with the
container.namespace('namespace') do |namespace|
  register(:one) {}
  register(:two) {}
end
Gets a bit tedious with register every time
Piotr Solnica
@solnic
Aug 17 2015 14:44
not repeating namespaces would be a major win for me
and some nice convention for splitting things into smaller files
this would become a nice map of your system’s dependencies
Andy Holland
@AMHOL
Aug 17 2015 14:45
Yeah
What's the issue with load order?
Piotr Solnica
@solnic
Aug 17 2015 14:45
I forgot, it just crashed, didn’t have time to dig into it but I will do that sooner or later
Andy Holland
@AMHOL
Aug 17 2015 14:46
Was something registered that resolved something else?
If you figure it out log an issue and I'll sort it out
Piotr Solnica
@solnic
Aug 17 2015 14:47
will do :)
Andy Holland
@AMHOL
Aug 17 2015 14:47
Cheers
Andy Holland
@AMHOL
Aug 17 2015 19:33
@solnic DYT container.with_namespace(ns) do or container.namespace(ns) do
Or prefix
Like container.prefix(prefix, prefix_separator: '.') do |container|
Andy Holland
@AMHOL
Aug 17 2015 20:34
Went with #namespace
@solnic WDYT of this:
container.register(:namespace) do |container|
  container.register(:test) { 'test' }
end
# registers as (string) 'namespace.test'
Piotr Solnica
@solnic
Aug 17 2015 20:36
oh this is lovely
Andy Holland
@AMHOL
Aug 17 2015 20:36
Cool
dryrb/dry-container#3 needs specs but WDYT of implementation?
Would be nice to do it without a decorator but couldn't think of anything that allows the same flexibility
Andy Holland
@AMHOL
Aug 17 2015 21:46
@solnic I'm not sure whether registering with a hash would be better than this namespace stuff
Gets a bit messy with nesting
i.e.
container.namespace('one') do
  namespace('two') { register('three') }
end
vs
container.register(one: {
  two: 'three'
})
Piotr Solnica
@solnic
Aug 17 2015 22:25
@AMHOL I'll experiment and see what's better. My container file is close to 200loc now
Andy Holland
@AMHOL
Aug 17 2015 22:25
Cool, not getting far with this
Andy Holland
@AMHOL
Aug 17 2015 22:31

      def namespace(namespace, &block)
        NamespaceDSL.new(self, namespace, &block)
      end
    end

    class NamespaceDSL < ::SimpleDelegator
      def initialize(container, namespace, &block)
        @namespace = "#{namespace}."
        super(container)

        if block.arity.zero?
          self.instance_eval(&block)
        else
          block.call(self)
        end
      end

      def register(key, *args, &block)
        super([@namespace, key].join(''), *args, &block)
      end

      def namespace(namespace, &block)
        super([@namespace, namespace].join(''), &block)
      end
    end
@solnic bit messy
WDYT?

      def namespace(namespace, &block)
        NamespaceDSL.new(self, namespace, &block)
      end
    end

    class NamespaceDSL < ::SimpleDelegator
      def initialize(container, namespace, &block)
        @namespace = namespace
        super(container)

        if block.arity.zero?
          self.instance_eval(&block)
        else
          block.call(self)
        end
      end

      def register(key, *args, &block)
        puts namespaced(key)
        super(namespaced(key), *args, &block)
      end

      def namespace(namespace, &block)
        super(namespaced(namespace), &block)
      end

      private

      def namespaced(key)
        "#{@namespace}.#{key}"
      end
    end
Bit cleaner
Piotr Solnica
@solnic
Aug 17 2015 22:51
On mobile. In Gotham. Will take a look in the morning :)
Andy Holland
@AMHOL
Aug 17 2015 22:51
Cool :p
Andy Holland
@AMHOL
Aug 17 2015 23:02
dryrb/dry-container#4
Pretty happy with that now
Andy Holland
@AMHOL
Aug 17 2015 23:12
Got an idea for splitting
Dry::Container::Namespace.new(ns, &block) and #import