Sha256: bb156b7553e11b00eddf3df7570ab1093600645c34adade11ccc578c4c561ea8

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module PipeRpc
  class BasicInterface < BasicObject
    class << self
      def const_missing(name)
        ::Object.const_get(name)
      end

      def instance_eval_for(object, *args, &block)
        BasicInterface.add_instance_eval
        object.instance_eval(*args, &block)
      ensure
        BasicInterface.remove_instance_eval
      end

      def remove_instance_eval
        class_eval do
          def instance_eval(*args)
            caller = (Kernel.respond_to? :caller) ? [Kernel.caller(1)] : nil
            Kernel.raise ::NoMethodError, "undefined method `instance_eval'", *caller
          end
        end
      end

      def add_instance_eval
        __send__(:remove_method, :instance_eval)
      end
    end

    wanted_methods = [:initialize, :__id__, :object_id, :__send__, :respond_to?, :method_missing]
    wanted_methods += (Object.const_defined? :MRUBY_VERSION) ? [:!] : [:!, :==, :!=, :equal?]

    existing_methods = BasicObject.all_instance_methods

    # Remove unwanted methods (while treating #instance_eval specially so we
    # can selectively use it)
    unwanted_methods = existing_methods - wanted_methods
    (unwanted_methods - [:instance_eval]).each { |m| undef_method m }
    remove_instance_eval

    # Add non-existing methods by including sorted out Kernel module
    include Kernel.dup_including *(wanted_methods - existing_methods)

    alias send __send__
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pipe_rpc-2.5.0 lib/pipe_rpc/basic_interface.rb