Module: Lexicon::Common::Mixin::Finalizable

Included in:
ShellExecutor
Defined in:
lib/lexicon/common/mixin/finalizable.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/lexicon/common/mixin/finalizable.rb', line 5

def self.included(base)
  class << base
    alias_method :_new, :new

    def new(*args, **options)
      e = do_call(self, '_new', args, options)

      ObjectSpace.define_finalizer(e, e.method(:_finalize))

      e
    end

    # Empty Array and Hash splats are handled correctly starting ruby 2.7:
    # if both args and kwargs are empty, no parameters are sent.
    if ::Semantic::Version.new(RUBY_VERSION).satisfies?('>= 2.7.0')
      private def do_call(obj, method, args, kwargs)
        obj.send(method, *args, **kwargs)
      end
    else
      private def do_call(obj, method, args, kwargs)
        if args.empty? && kwargs.empty?
          obj.send(method)
        elsif args.empty?
          obj.send(method, **kwargs)
        elsif kwargs.empty?
          obj.send(method, *args)
        else
          obj.send(method, *args, **kwargs)
        end
      end
    end
  end
end