Sha256: 50bcbfa5f610e7fad9bfa428ac9d5fdfc87119abbceb18e9fd3fcf4716b4b586

Contents?: true

Size: 1.88 KB

Versions: 31

Compression:

Stored size: 1.88 KB

Contents

# encoding: utf-8
module Mongoid
  class Criteria
    module Queryable

      # Allows for easy delegation of queryable queryable instance methods to a
      # specific method.
      module Forwardable

        # Tells queryable with method on the class to delegate to when calling an
        # original selectable or optional method on the class.
        #
        # @example Tell queryable where to select from.
        #   class Band
        #     extend Queryable::Forwardable
        #     select_with :criteria
        #
        #     def self.criteria
        #       Query.new
        #     end
        #   end
        #
        # @param [ Symbol ] receiver The name of the receiver method.
        #
        # @return [ Array<Symbol> ] The names of the forwarded methods.
        #
        # @since 1.0.0
        def select_with(receiver)
          (Selectable.forwardables + Optional.forwardables).each do |name|
            __forward__(name, receiver)
          end
        end

        private

        # Forwards the method name to the provided receiver method.
        #
        # @api private
        #
        # @example Define the forwarding.
        #   Model.__forward__(:exists, :criteria)
        #
        # @param [ Symbol ] name The name of the method.
        # @param [ Symbol ] receiver The name of the receiver method.
        #
        # @since 1.0.0
        def __forward__(name, receiver)
          if self.class == Module
            module_eval <<-SEL, __FILE__, __LINE__ + 1
              def #{name}(*args, &block)
                #{receiver}.__send__(:#{name}, *args, &block)
              end
            SEL
          else
            singleton_class.class_eval <<-SEL, __FILE__, __LINE__ + 1
              def #{name}(*args, &block)
                #{receiver}.__send__(:#{name}, *args, &block)
              end
            SEL
          end
        end
      end
    end
  end
end

Version data entries

31 entries across 29 versions & 2 rubygems

Version Path
mongoid-7.0.13 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.12 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-6.4.8 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.11 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.10 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-6.4.7 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-6.4.5 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.8 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.7 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.6 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.5 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.4 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-6.4.4 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.3 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-6.4.2 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.2 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.1 lib/mongoid/criteria/queryable/forwardable.rb
mongoid-6.4.1 lib/mongoid/criteria/queryable/forwardable.rb
tdiary-5.0.8 vendor/bundle/gems/mongoid-6.4.0/lib/mongoid/criteria/queryable/forwardable.rb
mongoid-7.0.0 lib/mongoid/criteria/queryable/forwardable.rb