Sha256: 67f9413da2b46979f75c6b065e85f8c7f7aca84aa7f723bf696cd055017140de

Contents?: true

Size: 1005 Bytes

Versions: 1

Compression:

Stored size: 1005 Bytes

Contents

class TransparentProxy

  VERSION = '0.0.5'

  proxy_methods = [:class, :methods, :respond_to?]

  proxy_methods.each { |m| alias_method "proxy_#{m}".to_sym, m }
  
  safe_methods = [:__send__, :send, :__id__, :object_id, :tap] + proxy_methods.map { |m| "proxy_#{m}".to_sym }

  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=nil, &block)
    @lazy_object = block_given? ? block : object
  end

  def __getobj__
    @object ||= @lazy_object.is_a?(Proc) ? @lazy_object.call : @lazy_object
  end

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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transparent_proxy-0.0.5 lib/transparent_proxy.rb