Sha256: fa50cac228dc91c8315b25deb0b5fc3b0b912a9a434cb0f02033ea80ab406b5e

Contents?: true

Size: 1022 Bytes

Versions: 1

Compression:

Stored size: 1022 Bytes

Contents

module Asynchronic
  class TransparentProxy

    PROXY_METHODS = [:class, :methods, :respond_to?]
    
    SAFE_METHODS = [:__send__, :__id__, :object_id, :tap] + PROXY_METHODS.map { |m| "proxy_#{m}".to_sym }

    PROXY_METHODS.each { |m| alias_method "proxy_#{m}".to_sym, m }

    instance_methods.reject { |m| SAFE_METHODS.include? m }.
                     each   { |m| undef_method m }

    def inspect
      __getobj__.inspect
    end

    def proxy_inspect
      "#<#{proxy_class} @object=#{inspect}>"
    end

    def methods(*args)
      proxy_methods(*args) | __getobj__.methods(*args)
    end

    def respond_to?(*args)
      proxy_respond_to?(*args) || __getobj__.respond_to?(*args)
    end

    def proxy?
      true
    end

    private

    def initialize(object)
      __setobj__ object
    end

    def __getobj__
      @object
    end

    def __setobj__(object)
      @object = object
    end

    def method_missing(method, *args, &block)
      __getobj__.send(method, *args, &block)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asynchronic-0.2.0 lib/asynchronic/transparent_proxy.rb