Sha256: 66b1e67703aef9889e84062ad28fb3bd2c8da9f0b6f7827277db9f33ee51dbcf

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'set'
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?]).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

1 entries across 1 versions & 1 rubygems

Version Path
dumb_delegator-0.7.0 lib/dumb_delegator.rb