Sha256: 4f3eec3fcf48c7b4061b57272cc9352d67dea6670b1254c9c09b8f0b650ad5e5

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

module Draper
  module AutomaticDelegation
    extend ActiveSupport::Concern

    # Delegates missing instance methods to the source object.
    def method_missing(method, *args, &block)
      return super unless delegatable?(method)

      self.class.delegate method
      send(method, *args, &block)
    end

    # Checks if the decorator responds to an instance method, or is able to
    # proxy it to the source object.
    def respond_to_missing?(method, include_private = false)
      super || delegatable?(method)
    end

    # @private
    def delegatable?(method)
      object.respond_to?(method)
    end

    module ClassMethods
      # Proxies missing class methods to the source class.
      def method_missing(method, *args, &block)
        return super unless delegatable?(method)

        object_class.send(method, *args, &block)
      end

      # Checks if the decorator responds to a class method, or is able to proxy
      # it to the source class.
      def respond_to_missing?(method, include_private = false)
        super || delegatable?(method)
      end

      # @private
      def delegatable?(method)
        object_class? && object_class.respond_to?(method)
      end

      # @private
      # Avoids reloading the model class when ActiveSupport clears autoloaded
      # dependencies in development mode.
      def before_remove_const
      end
    end

    included do
      private :delegatable?
      private_class_method :delegatable?
    end

  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
draper_new-3.0.0 lib/draper/automatic_delegation.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/draper-2.1.0/lib/draper/automatic_delegation.rb
draper-2.1.0 lib/draper/automatic_delegation.rb
draper-2.0.0 lib/draper/automatic_delegation.rb
draper-1.4.0 lib/draper/automatic_delegation.rb
draper-1.3.1 lib/draper/automatic_delegation.rb
draper-1.3.0 lib/draper/automatic_delegation.rb
draper-1.2.1 lib/draper/automatic_delegation.rb