Sha256: 96e2d477e189b38e30caec383ade723d18b59b42c900f6c3379d56e2bd6afd3c

Contents?: true

Size: 760 Bytes

Versions: 1

Compression:

Stored size: 760 Bytes

Contents

module Casting
  module MissingMethodClient

    def cast_as(attendant)
      __delegates__.unshift(attendant)
      self
    end

    def uncast
      __delegates__.shift
      self
    end

    private

    def __delegates__
      @__delegates__ ||= []
    end

    def method_missing(meth, *args, &block)
      if !!method_delegate(meth)
        delegate(meth, method_delegate(meth), *args, &block)
      else
        super
      end
    end

    def respond_to_missing?(meth, *)
      !!method_delegate(meth) || super
    end

    def method_delegate(meth)
      __delegates__.find{|attendant|
        if Module === attendant
          attendant.instance_methods
        else
          attendant.methods
        end.include?(meth)
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
casting-0.6.1 lib/casting/missing_method_client.rb