Sha256: f641f02b9fe74b385d4a58014baaf4e8bdd68eb25dd7adec5bcb5f6433b54f2b

Contents?: true

Size: 1.73 KB

Versions: 38

Compression:

Stored size: 1.73 KB

Contents

# encoding: utf-8
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)
      #
      # @param [ Class ] klass The document class.
      # @param [ Symbol ] name The method name.
      def initialize(klass, name)
        super(
          compose_message(
            "invalid_field",
            {
              name: name,
              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.
      #
      # @since 3.0.0
      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.
      #
      # @since 3.0.0
      def location(klass, name)
        @location ||=
          (klass.instance_method(name).source_location || [ "Unknown", 0 ])
      end
    end
  end
end

Version data entries

38 entries across 36 versions & 2 rubygems

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