Module: Dry::Container::Nested::ClassInterface

Includes:
Forwardable
Included in:
Dry::Container::Nested
Defined in:
lib/dry/container/nested.rb

Overview

Class-level DSL for nested container definition

Instance Method Summary collapse

Instance Method Details

#apply_namespace!Object



121
122
123
124
125
# File 'lib/dry/container/nested.rb', line 121

def apply_namespace!
  key_methods.map do |method|
    redefine_method method
  end
end

#key_methods<UnboundMethod> (private)

Methods accepting key as one of their parameters

Returns:

  • (<UnboundMethod>)


131
132
133
134
135
# File 'lib/dry/container/nested.rb', line 131

def key_methods
  Mixin.instance_methods(false).inject([]) do |result, method|
    key_parameter(method) ? result + [method] : result
  end
end

#key_parameter(method_or_name) ⇒ Object (private)

Parameters:

  • method_or_name (UnboundMethod)


138
139
140
141
# File 'lib/dry/container/nested.rb', line 138

def key_parameter(method_or_name)
  method_or_name = method_definition(method_or_name)
  method_or_name.parameters.detect { |_kind, name| :key == name }
end

#method_definition(method_or_name, from: Mixin) ⇒ UnboundMethod (private)

Parameters:

  • method_or_name (UnboundMethod, Symbol)
  • from (Module) (defaults to: Mixin)

Returns:

  • (UnboundMethod)


146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dry/container/nested.rb', line 146

def method_definition(method_or_name, from: Mixin)
  if method_or_name.is_a?(Symbol)
    method_or_name = from.instance_method method_or_name
  end
  unless method_or_name.is_a? UnboundMethod
    raise ArgumentError,
          "#{method_or_name} is not an unbound method or symbol "\
          "found in #{from}",
          caller.join("\n")
  end
  method_or_name
end

#redefine_method(method) ⇒ Object (private)

Parameters:

  • method (UnboundMethod)


160
161
162
163
164
165
166
167
168
169
# File 'lib/dry/container/nested.rb', line 160

def redefine_method(method)
  method = method_definition(method)
  key_parameter = key_parameter(method)
  index = method.parameters.index(key_parameter)
  define_method method.name do |*args, **kwargs|
    args = args.dup
    args[index] = prefixed(args[index])
    container.send(method.name, *args, **kwargs)
  end
end