Module: Dry::BlackTie

Defined in:
lib/dry/behaviour/black_tie.rb

Overview

rubocop:disable Style/MultilineBlockChain rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize rubocop:disable Style/MethodName

Constant Summary collapse

PARAM_TYPES =
%i[req opt rest key keyrest keyreq block]
DELEGATE_METHOD =
lambda do |klazz, (source, target)|
  klazz.class_eval do
    define_method(source, &Dry::DEFINE_METHOD.curry[target])
  end
end
POSTPONE_EXTEND =
lambda do |target, protocol|
  TracePoint.new(:end) do |tp|
    if tp.self == protocol
      target.extend protocol
      tp.disable
    end
  end.enable
end
NORMALIZE_KEYS =
lambda do |protocol|
  BlackTie.protocols[protocol].keys.reject { |k| k.to_s =~ /\A__.*__\z/ }
end
IMPLICIT_DELEGATE_DEPRECATION =
"\n🚨️  DEPRECATED →  %s\n" \
"  ⮩  Implicit delegation to the target class will be removed in 1.0\n" \
"  ⮩   due to the lack of the explicit implementation of %s#%s for %s\n" \
"  ⮩   it will be delegated to the target class itself.\n" \
"  ⮩  Consider using explicit `delegate:' declaration in `defimpl' or\n" \
"  ⮩   use `implicit_inheritance: true' parameter in protocol definition.".freeze
IMPLICIT_RECEIVER_DECLARATION =
"\n⚠️  TOO IMPLICIT →  %s\n" \
"  ⮩  Implicit declaration of `this' parameter in `defmethod'.\n" \
"  ⮩   Whilst it’s allowed, we strongly encourage to explicitly declare it\n" \
'  ⮩   in call to %s#defmethod(%s).'.freeze
UNKNOWN_TYPE_DECLARATION =
"\n⚠️  UNKNOWN TYPE →  %s\n" \
"  ⮩  Unknown parameter type [%s] in call to %s#defmethod(%s).\n" \
"  ⮩   Is it a typo? Omit the type for `:req' or pass one of allowed types:\n" \
"  ⮩   #{PARAM_TYPES.inspect}".freeze
WRONG_PARAMETER_DECLARATION =
"\n🚨️  DEPRECATED →  %s\n" \
"  ⮩  Wrong parameters declaration will be removed in 1.0\n" \
"  ⮩   %s#%s was implemented for %s with unexpected parameters.\n" \
"  ⮩  Consider implementing interfaces exactly as they were declared.\n" \
'  ⮩   Expected: %s'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defimpl(protocol = nil, target: nil, delegate: [], map: {}, &λ) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/dry/behaviour/black_tie.rb', line 114

def defimpl(protocol = nil, target: nil, delegate: [], map: {}, &λ)
  raise if target.nil? || !block_given? && delegate.empty? && map.empty?

  mds = normalize_map_delegates(delegate, map)

  Module.new do
    mds.each(&DELEGATE_METHOD.curry[singleton_class])
    singleton_class.class_eval(&λ) if block_given? # block takes precedence
  end.tap do |mod|
    protocol ? mod.extend(protocol) : POSTPONE_EXTEND.(mod, protocol = self)

    mod.methods(false).tap do |meths|
      (NORMALIZE_KEYS.(protocol) - meths).each_with_object(meths) do |m, acc|
        if BlackTie.protocols[protocol][:__implicit_inheritance__]
          mod.singleton_class.class_eval do
            define_method m do |this, *♿_args, **♿_kwargs, &♿_λ|
              # [AM] [v1] [FIXME] for modern rubies `if` is redundant
              if ♿_kwargs.empty?
                super(this, *♿_args, &♿_λ)
              else
                super(this, *♿_args, **♿_kwargs, &♿_λ)
              end
            end
          end
        else
          BlackTie.Logger.warn(
            format(IMPLICIT_DELEGATE_DEPRECATION, Dry::BlackTie.proto_caller, protocol.inspect, m, target)
          )
          DELEGATE_METHOD.(mod.singleton_class, [m] * 2)
        end
        acc << m
      end
    end.each do |m|
      target = [target] unless target.is_a?(Array)
      target.each do |tgt|
        params = mod.method(m).parameters.reject { |_, v| v.to_s[/\A♿_/] }
        proto = BlackTie.protocols[protocol]
        ok =
          mds.map(&:first).include?(m) ||
          ((proto[m] == {} || proto[:__implicit_inheritance__]) && [[:req],
                                                                    [:rest]].include?(params.map(&:first))) ||
          [proto[m], params].map { |args| args.map(&:first) }.reduce(:==)

        # TODO[1.0] raise NotImplemented(:arity)
        unless ok
          BlackTie.Logger.warn(
            format(WRONG_PARAMETER_DECLARATION, Dry::BlackTie.proto_caller, protocol.inspect, m, target,
                   BlackTie.protocols[protocol][m].map(&:first))
          )
        end

        BlackTie.implementations[protocol][tgt][m] = mod.method(m).to_proc
      end
    end
  end
end

.implementationsObject



30
31
32
# File 'lib/dry/behaviour/black_tie.rb', line 30

def implementations
  @implementations ||= Hash.new { |h, k| h[k] = h.dup.clear }
end

.LoggerObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/dry/behaviour/black_tie.rb', line 15

def Logger
  @logger ||=
    if Kernel.const_defined?('::Rails')
      Rails.logger
    else
      require 'logger'
      Logger.new($stdout)
    end
  @logger || Class.new { def warn(*); end }.new
end

.normalize_map_delegates(delegate, map) ⇒ Object



222
223
224
225
226
227
228
229
# File 'lib/dry/behaviour/black_tie.rb', line 222

def normalize_map_delegates(delegate, map)
  [*delegate, *map].map do |e|
    case e
    when Symbol, String then [e.to_sym] * 2
    when Array then e.map(&:to_sym) if e.size == 2
    end
  end.compact
end

.proto_callerObject



9
10
11
12
13
# File 'lib/dry/behaviour/black_tie.rb', line 9

def proto_caller
  caller.drop_while do |line|
    line =~ %r{dry-behaviour/lib/dry/behaviour}
  end.first
end

.protocolsObject



26
27
28
# File 'lib/dry/behaviour/black_tie.rb', line 26

def protocols
  @protocols ||= Hash.new { |h, k| h[k] = h.dup.clear }
end

Instance Method Details

#defmethod(name, *params) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dry/behaviour/black_tie.rb', line 98

def defmethod(name, *params)
  if params.size.zero? || params.first.is_a?(Array) && params.first.last != :req
    BlackTie.Logger.warn(format(IMPLICIT_RECEIVER_DECLARATION, Dry::BlackTie.proto_caller, inspect, name))
    params.unshift(:this)
  end
  params =
    params.map do |p, type|
      if type && !PARAM_TYPES.include?(type)
        BlackTie.Logger.warn(format(UNKNOWN_TYPE_DECLARATION, Dry::BlackTie.proto_caller, type, inspect, name))
        type = nil
      end
      [type || (PARAM_TYPES.include?(p) ? p : :req), p]
    end
  BlackTie.protocols[self][name] = params
end

#defprotocol(implicit_inheritance: false, &λ) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dry/behaviour/black_tie.rb', line 35

def defprotocol(implicit_inheritance: false, &λ)
  raise ::Dry::Protocol::DuplicateDefinition, self if BlackTie.protocols.key?(self)
  raise ::Dry::Protocol::MalformedDefinition, self unless block_given?

  BlackTie.protocols[self][:__implicit_inheritance__] = !!implicit_inheritance

  ims = instance_methods(false)
  class_eval(&λ)
  (instance_methods(false) - ims).each { |m| class_eval { module_function m } }

  singleton_class.send :define_method, :method_missing do |method, *_args|
    raise Dry::Protocol::NotImplemented.new(
      :method, inspect, method: method, self: self
    )
  end

  singleton_class.send :define_method, :implementation_for do |receiver|
    receiver.class.ancestors.lazy.map do |c|
      BlackTie.implementations[self].fetch(c, nil)
    end.reject(&:nil?).first
  end

  BlackTie.protocols[self].each do |method, *_m_args, **_m_kwargs| # FIXME: CHECK ARITY HERE
    # singleton_class.send :define_method, method do |receiver, *args, **kwargs|
    singleton_class.send :define_method, method do |*args, **kwargs|
      if args == []
        receiver = kwargs
        kwargs = {}
      else
        receiver, *args = args
      end

      impl = implementation_for(receiver)

      unless impl
        raise Dry::Protocol::NotImplemented.new(
          :protocol, inspect,
          method: method, receiver: receiver, args: args, self: self
        )
      end

      begin
        # [AM] [v1] [FIXME] for modern rubies `if` is redundant
        if kwargs.empty?
          impl[method].(*args.unshift(receiver))
        else
          impl[method].(*args.unshift(receiver), **kwargs)
        end
      rescue StandardError => e
        raise Dry::Protocol::NotImplemented.new(
          :nested, inspect,
          cause: e,
          method: method, receiver: receiver, args: args, impl: impl, self: self
        )
      end
    end
  end

  singleton_class.send :define_method, :respond_to? do |method|
    NORMALIZE_KEYS.(self).include? method
  end
end