Sha256: d61f88531d96922b9d53d6e0bb685b41e81f3daf1baf0b399af5e5c084f678aa
Contents?: true
Size: 1.26 KB
Versions: 115
Compression:
Stored size: 1.26 KB
Contents
# frozen_string_literal: true require 'active_support/inflector' require 'eac_ruby_utils/ruby/on_replace_objects/replace_instance_method' module EacRubyUtils module Ruby class << self def on_replace_objects replacer = OnReplaceObjects.new replacer.on_replacement do yield(replacer) end end end class OnReplaceObjects def on_replacement(&block) clear_replacements block.call(self) ensure restore_replacements end def replace_instance_method(a_module, method_name, &block) add_replacement(__method__, a_module, method_name, &block) end def replace_self_method(object, method_name, &block) add_replacement(:replace_instance_method, object.singleton_class, method_name, &block) end private def add_replacement(method_name, *args, &block) @replacements << replacement_class(method_name).new(*args, &block).apply end def replacement_class(method_name) self.class.const_get(::ActiveSupport::Inflector.camelize(method_name)) end def clear_replacements @replacements = [] end def restore_replacements @replacements.reverse.each(&:restore) end end end end
Version data entries
115 entries across 115 versions & 2 rubygems