Sha256: 0fbb2f9ab3f4aba53dc02ec8f018c85cea0081cc86a66b218bb4b524c0a0e52e

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'dumb_delegator/version'

class DumbDelegator < ::BasicObject
  (::BasicObject.instance_methods - [:equal?, :__id__, :__send__, :method_missing]).each do |method|
    undef_method method
  end

  kernel = ::Kernel.dup
  (kernel.instance_methods - [:dup, :clone, :respond_to?, :object_id]).each do |method|
    kernel.__send__ :undef_method, method
  end
  include kernel

  def initialize(target)
    __setobj__(target)
  end

  def respond_to?(method, include_all=false)
    __getobj__.respond_to?(method) || super
  end

  def method_missing(method, *args, &block)
    if @__dumb_target__.respond_to?(method)
      @__dumb_target__.__send__(method, *args, &block)
    else
      super
    end
  end

  def __getobj__
    @__dumb_target__
  end

  def __setobj__(obj)
    raise ::ArgumentError, 'Delegation to self is not allowed.' if obj.__id__ == __id__
    @__dumb_target__ = obj
  end

  def marshal_dump
    [
      :__v1__,
      __getobj__
    ]
  end

  def marshal_load(data)
    version, obj = data
    case version
    when :__v1__
      __setobj__(obj)
    end
  end

  private

  def initialize_dup(obj)
    __setobj__(obj.__getobj__.dup)
  end

  def initialize_clone(obj)
    __setobj__(obj.__getobj__.clone)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dumb_delegator-0.8.1 lib/dumb_delegator.rb
dumb_delegator-0.8.0 lib/dumb_delegator.rb