File: namespace_dsl.rb

package info (click to toggle)
ruby-dry-container 0.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 236 kB
  • sloc: ruby: 976; makefile: 4
file content (59 lines) | stat: -rw-r--r-- 1,285 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'delegate'

module Dry
  class Container
    # @api private
    class NamespaceDSL < ::SimpleDelegator
      # DSL for defining namespaces
      #
      # @param [Dry::Container::Mixin] container
      #   The container
      # @param [String] namespace
      #   The namespace (name)
      # @param [String] namespace_separator
      #   The namespace separator
      # @yield
      #   The block to evaluate to define the namespace
      #
      # @return [Mixed]
      #
      # @api private
      def initialize(container, namespace, namespace_separator, &block)
        @namespace = namespace
        @namespace_separator = namespace_separator

        super(container)

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

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

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

      def import(namespace)
        namespace(namespace.name, &namespace.block)

        self
      end

      def resolve(key)
        super(namespaced(key))
      end

      private

      def namespaced(key)
        [@namespace, key].join(@namespace_separator)
      end
    end
  end
end