File: method_patch_macro.rb

package info (click to toggle)
ruby-librarian 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 624 kB
  • sloc: ruby: 6,109; makefile: 11
file content (30 lines) | stat: -rw-r--r-- 616 bytes parent folder | download | duplicates (7)
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
require "securerandom"

module Support
  module MethodPatchMacro

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def with_module_method(mod, meth, &block)
        tag = SecureRandom.hex(8)
        orig_meth = "_#{tag}_#{meth}".to_sym
        before do
          mod.module_eval do
            alias_method orig_meth, meth
            define_method meth, &block
          end
        end
        after do
          mod.module_eval do
            alias_method meth, orig_meth
            remove_method orig_meth
          end
        end
      end
    end

  end
end