Sha256: ee1719beba234ef5a81833c203db74459a025d277bbd5de8f19867f4d5bcb91f

Contents?: true

Size: 1.21 KB

Versions: 21

Compression:

Stored size: 1.21 KB

Contents

module ActiveFedora
  module Delegation # :nodoc:
    extend ActiveSupport::Concern

    # This module creates compiled delegation methods dynamically at runtime, which makes
    # subsequent calls to that method faster by avoiding method_missing. The delegations
    # may vary depending on the klass of a relation, so we create a subclass of Relation
    # for each different klass, and the delegations are compiled into that subclass only.

    BLACKLISTED_ARRAY_METHODS = [
      :compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!,
      :shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
      :keep_if, :pop, :shift, :delete_at, :select!
    ].to_set

    delegate :length, :collect, :map, :each, :all?, :include?, :to_ary, to: :to_a

    protected

      def array_delegable?(method)
        Array.method_defined?(method) && BLACKLISTED_ARRAY_METHODS.exclude?(method)
      end

      def method_missing(method, *args, &block)
        if array_delegable?(method)
          self.class.delegate method, to: :to_a
          to_a.public_send(method, *args, &block)
        else
          super
        end
      end

      def respond_to_missing?(method, *args)
        array_delegable?(method) || super
      end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
active-fedora-11.2.1 lib/active_fedora/relation/delegation.rb
active-fedora-12.0.3 lib/active_fedora/relation/delegation.rb
active-fedora-12.0.2 lib/active_fedora/relation/delegation.rb
active-fedora-12.0.1 lib/active_fedora/relation/delegation.rb
active-fedora-11.5.2 lib/active_fedora/relation/delegation.rb
active-fedora-12.0.0 lib/active_fedora/relation/delegation.rb
active-fedora-11.5.0 lib/active_fedora/relation/delegation.rb
active-fedora-11.4.1 lib/active_fedora/relation/delegation.rb
active-fedora-11.4.0 lib/active_fedora/relation/delegation.rb
active-fedora-11.3.1 lib/active_fedora/relation/delegation.rb
active-fedora-11.3.0 lib/active_fedora/relation/delegation.rb
active-fedora-11.2.0 lib/active_fedora/relation/delegation.rb
active-fedora-11.1.6 lib/active_fedora/relation/delegation.rb
active-fedora-11.1.5 lib/active_fedora/relation/delegation.rb
active-fedora-11.1.4 lib/active_fedora/relation/delegation.rb
active-fedora-11.1.3 lib/active_fedora/relation/delegation.rb
active-fedora-11.1.2 lib/active_fedora/relation/delegation.rb
active-fedora-11.1.1 lib/active_fedora/relation/delegation.rb
active-fedora-11.1.0 lib/active_fedora/relation/delegation.rb
active-fedora-11.0.1 lib/active_fedora/relation/delegation.rb