Sha256: bd8be33033990e3cb28624d72e11a3552a0a44ed546bdda76ab91f229b9dea44

Contents?: true

Size: 886 Bytes

Versions: 15

Compression:

Stored size: 886 Bytes

Contents

module Mocktail
  class RedefinesNew
    def initialize
      @handles_dry_new_call = HandlesDryNewCall.new
    end

    def redefine(type)
      type_replacement = TopShelf.instance.type_replacement_for(type)

      if type_replacement.replacement_new.nil?
        type_replacement.original_new = type.method(:new)
        type.singleton_class.send(:undef_method, :new)
        handles_dry_new_call = @handles_dry_new_call
        type.define_singleton_method :new, ->(*args, **kwargs, &block) {
          if TopShelf.instance.new_replaced?(type) ||
              TopShelf.instance.of_next_registered?(type)
            handles_dry_new_call.handle(type, args, kwargs, block)
          else
            type_replacement.original_new.call(*args, **kwargs, &block)
          end
        }
        type_replacement.replacement_new = type.singleton_method(:new)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
mocktail-1.2.3 lib/mocktail/replaces_type/redefines_new.rb
mocktail-1.2.2 lib/mocktail/replaces_type/redefines_new.rb
mocktail-1.2.1 lib/mocktail/replaces_type/redefines_new.rb
mocktail-1.2.0 lib/mocktail/replaces_type/redefines_new.rb
mocktail-1.1.3 lib/mocktail/replaces_type/redefines_new.rb
mocktail-1.1.2 lib/mocktail/replaces_type/redefines_new.rb
mocktail-1.1.1 lib/mocktail/replaces_type/redefines_new.rb
mocktail-1.1.0 lib/mocktail/replaces_type/redefines_new.rb
mocktail-1.0.0 lib/mocktail/replaces_type/redefines_new.rb
mocktail-0.0.6 lib/mocktail/replaces_type/redefines_new.rb
mocktail-0.0.5 lib/mocktail/replaces_type/redefines_new.rb
mocktail-0.0.4 lib/mocktail/replaces_type/redefines_new.rb
mocktail-0.0.3 lib/mocktail/replaces_type/redefines_new.rb
mocktail-0.0.2 lib/mocktail/replaces_type/redefines_new.rb
mocktail-0.0.1 lib/mocktail/replaces_type/redefines_new.rb