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

Version Path
eac_ruby_utils-0.124.0 lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.97.2 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_ruby_utils-0.123.0 lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_ruby_utils-0.121.0 lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_ruby_utils-0.120.0 lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.76.1 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.76.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.75.2 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_ruby_utils-0.119.2 lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.75.1 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.75.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_ruby_utils-0.119.1 lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.74.1 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.74.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.73.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_ruby_utils-0.119.0 lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.72.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_ruby_utils-0.118.1 lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_ruby_utils-0.118.0 lib/eac_ruby_utils/ruby/on_replace_objects.rb
eac_tools-0.70.1 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_replace_objects.rb