Sha256: e2908cfd4a465cf16cdaa79c07855cd9acc2895257a14d8a4c71e3386f588835

Contents?: true

Size: 1.79 KB

Versions: 18

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Errors

    # This error is raised when trying to create a field that conflicts with
    # an already defined method.
    class InvalidField < MongoidError

      # Create the new error.
      #
      # @example Create the error.
      #   InvalidField.new(person, :crazy_method_name)
      #
      # @api private
      #
      # @param [ Class ] klass The document class.
      # @param [ Symbol ] field The field name.
      # @param [ Symbol ] name The method name.
      def initialize(klass, field, name)
        super(
          compose_message(
            "invalid_field",
            {
              name: name,
              field: field,
              origin: origin(klass, name),
              file: location(klass, name)[0],
              line: location(klass, name)[1]
            }
          )
        )
      end

      private

      # Get the queryable of the method.
      #
      # @example Get the originating class or module.
      #   error.queryable(Person, :crazy_method_name)
      #
      # @param [ Class ] klass The document class.
      # @param [ Symbol ] name The method name.
      #
      # @return [ Class | Module ] The originating class or module.
      def origin(klass, name)
        klass.instance_method(name).owner
      end

      # Get the location of the method.
      #
      # @example Get the location of the method on the filesystem.
      #   error.location(Person, :crazy_method_name)
      #
      # @param [ Class ] klass The document class.
      # @param [ Symbol ] name The method name.
      #
      # @return [ Array<String, Integer> ] The location of the method.
      def location(klass, name)
        @location ||=
          (klass.instance_method(name).source_location || [ "Unknown", 0 ])
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
mongoid-8.1.9 lib/mongoid/errors/invalid_field.rb
mongoid-8.0.9 lib/mongoid/errors/invalid_field.rb
mongoid-8.1.8 lib/mongoid/errors/invalid_field.rb
mongoid-8.1.7 lib/mongoid/errors/invalid_field.rb
mongoid-8.1.6 lib/mongoid/errors/invalid_field.rb
mongoid-8.0.8 lib/mongoid/errors/invalid_field.rb
mongoid-8.1.5 lib/mongoid/errors/invalid_field.rb
mongoid-8.1.4 lib/mongoid/errors/invalid_field.rb
mongoid-8.0.7 lib/mongoid/errors/invalid_field.rb
mongoid-8.1.3 lib/mongoid/errors/invalid_field.rb
mongoid-8.1.2 lib/mongoid/errors/invalid_field.rb
mongoid-8.0.6 lib/mongoid/errors/invalid_field.rb
mongoid-8.1.1 lib/mongoid/errors/invalid_field.rb
mongoid-8.0.5 lib/mongoid/errors/invalid_field.rb
mongoid-8.1.0 lib/mongoid/errors/invalid_field.rb
mongoid-8.0.4 lib/mongoid/errors/invalid_field.rb
mongoid-8.0.3 lib/mongoid/errors/invalid_field.rb
mongoid-8.0.2 lib/mongoid/errors/invalid_field.rb