Sha256: e98f6aa197ef56d3a8f5503643c182621285bc65ee96c6b31c8434dc90371729

Contents?: true

Size: 1.72 KB

Versions: 67

Compression:

Stored size: 1.72 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 origin of the method.
      #
      # @example Get the originating class or module.
      #   error.origin(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

67 entries across 64 versions & 7 rubygems

Version Path
mongoid-3.1.2 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.23 lib/mongoid/errors/invalid_field.rb
mongoid-3.1.1 lib/mongoid/errors/invalid_field.rb
mongoid-3.1.0 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.22 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.21 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.20 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.19 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.18 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.17 lib/mongoid/errors/invalid_field.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.16/lib/mongoid/errors/invalid_field.rb
mongoid-3.0.16 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.15 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.14 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.13 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.12 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.11 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.10 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.9 lib/mongoid/errors/invalid_field.rb
mongoid-3.0.6 lib/mongoid/errors/invalid_field.rb