Sha256: 1f1b6d421655daf8b617fddec0abfb186b2976df7729af140ac786cd52f8767f

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

require 'set'

module Bogus
  module HasOverwritenMethods
    def self.aliased_name(name)
      :"__bogus__alias__#{name}"
    end

    def self.alias(object, new_name, name)
      object.singleton_class.send(:alias_method, new_name, name)
    end

    def __overwritten_methods__
      @__overwritten_methods__ ||= Set.new
    end

    def __overwrite__(name, method, body)
      return if __overwritten_methods__.include?(name)

      new_name = HasOverwritenMethods.aliased_name(name)
      HasOverwritenMethods.alias(self, new_name, name) if method

      __overwritten_methods__ << name

      instance_eval(body)
    end

    def __reset__
      __overwritten_methods__.each do |name|
        new_name = HasOverwritenMethods.aliased_name(name)

        if respond_to?(new_name)
          HasOverwritenMethods.alias(self, name, new_name)
          instance_eval "undef #{new_name}"
        else
          instance_eval "undef #{name}"
        end
      end
      @__overwritten_methods__ = nil
      @__shadow__ = nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bogus-0.1.7 lib/bogus/stubbing/has_overwritten_methods.rb
bogus-0.1.6 lib/bogus/stubbing/has_overwritten_methods.rb
bogus-0.1.5 lib/bogus/stubbing/has_overwritten_methods.rb