Sha256: 9cafddc3b4ba8c9d56464e0134ac9e4f916f593357bd3e4b2dd7deb5908c1b76

Contents?: true

Size: 999 Bytes

Versions: 1

Compression:

Stored size: 999 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
          cast(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][name] ||= []
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
casting-1.0.2 lib/casting/missing_method_client_class.rb