Sha256: e839de4d8bd045f0fc947c8bfb2636dee26a05f15a5b24b908f9e6c49d59d49f

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 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]).each do |method|
    kernel.__send__ :undef_method, method
  end
  include kernel

  NON_DELEGATED_METHODS = [:equal?, :__id__, :__send__, :dup, :clone, :__getobj__, :__setobj__,
                           :marshal_dump, :marshal_load, :respond_to?].freeze

  def initialize(target)
    __setobj__(target)
  end

  def respond_to?(method)
    __getobj__.respond_to?(method) || NON_DELEGATED_METHODS.include?(method.to_sym)
  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)
    @__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.5.0 lib/dumb_delegator.rb
dumb_delegator-0.0.1 lib/dumb_delegator.rb