Sha256: 9a1a95e46d8710d12586680bf59b7ec854978984a1f6741dd2aec6c90c3fb430

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid

  # Contains the bahvior around inspecting documents via inspect.
  #
  # @since 4.0.0
  module Inspectable

    # Returns the class name plus its attributes. If using dynamic fields will
    # include those as well.
    #
    # @example Inspect the document.
    #   person.inspect
    #
    # @return [ String ] A nice pretty string to look at.
    #
    # @since 1.0.0
    def inspect
      inspection = []
      inspection.concat(inspect_fields).concat(inspect_dynamic_fields)
      "#<#{self.class.name} _id: #{_id}, #{inspection * ', '}>"
    end

    private

    # Get an array of inspected fields for the document.
    #
    # @api private
    #
    # @example Inspect the defined fields.
    #   document.inspect_fields
    #
    # @return [ String ] An array of pretty printed field values.
    #
    # @since 1.0.0
    def inspect_fields
      fields.map do |name, field|
        unless name == "_id"
          as = field.options[:as]
          "#{name}#{as ? "(#{as})" : nil}: #{@attributes[name].inspect}"
        end
      end.compact
    end

    # Get an array of inspected dynamic fields for the document.
    #
    # @api private
    #
    # @example Inspect the dynamic fields.
    #   document.inspect_dynamic_fields
    #
    # @return [ String ] An array of pretty printed dynamic field values.
    #
    # @since 1.0.0
    def inspect_dynamic_fields
      []
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mongoid-7.3.5 lib/mongoid/inspectable.rb
mongoid-7.3.4 lib/mongoid/inspectable.rb
mongoid-7.3.3 lib/mongoid/inspectable.rb
mongoid-7.3.2 lib/mongoid/inspectable.rb
mongoid-7.3.1 lib/mongoid/inspectable.rb
mongoid-7.3.0 lib/mongoid/inspectable.rb