Sha256: 1ca88615c85bb64578caac3b130331ef4ad2357b55238ed853b338b093335d88

Contents?: true

Size: 1006 Bytes

Versions: 3

Compression:

Stored size: 1006 Bytes

Contents

module Casting
  module MissingMethodClientClass

    def self.extended(base)
      base.send(:include, InstanceMethods)
    end

    module InstanceMethods
      private

      def __class_delegates__
        self.class.__delegates__
      end

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

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

      def method_class_delegate(meth)
        __class_delegates__.find{|attendant|
          attendant.method_defined?(meth)
        }
      end
    end

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

    def uncast
      __delegates__.shift
      self
    end

    def __delegates__
      Thread.current[:class_delegates] ||= {}
      Thread.current[:class_delegates][self.name] ||= []
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
casting-0.7.2 lib/casting/missing_method_client_class.rb
casting-0.7.1 lib/casting/missing_method_client_class.rb
casting-0.7.0 lib/casting/missing_method_client_class.rb