lib/syncem.rb in syncem-0.1.2 vs lib/syncem.rb in syncem-0.2.0

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true # (The MIT License) # -# Copyright (c) 2018-2019 Yegor Bugayenko +# Copyright (c) 2018-2023 Yegor Bugayenko # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the 'Software'), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -27,11 +27,11 @@ # # For more information read # {README}[https://github.com/yegor256/syncem/blob/master/README.md] file. # # Author:: Yegor Bugayenko (yegor256@gmail.com) -# Copyright:: Copyright (c) 2018-2019 Yegor Bugayenko +# Copyright:: Copyright (c) 2018-2023 Yegor Bugayenko # License:: MIT class SyncEm undef_method :send def initialize(origin) @@ -39,12 +39,21 @@ @mutex = Mutex.new end def method_missing(*args) @mutex.synchronize do - if @origin.respond_to?(args[0]) - @origin.__send__(*args) do |*a| - yield(*a) if block_given? + mtd = args.shift + if @origin.respond_to?(mtd) + params = @origin.method(mtd).parameters + reqs = params.count { |p| p[0] == :req } + if params.any? { |p| p[0] == :key } && args.size > reqs + @origin.__send__(mtd, *args[0...-1], **args.last) do |*a| + yield(*a) if block_given? + end + else + @origin.__send__(mtd, *args) do |*a| + yield(*a) if block_given? + end end else super end end