Sha256: 9e9ef2ddf0f357ee70deb7aa3f0ce23f2d69fe6090d8bc33be69e5cd801ff4c6

Contents?: true

Size: 1002 Bytes

Versions: 2

Compression:

Stored size: 1002 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][self.name] ||= []
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
casting-1.0.1 lib/casting/missing_method_client_class.rb
casting-1.0.0 lib/casting/missing_method_client_class.rb