Sha256: 2d9438e42f67ceae09d7c947e85a3bfaa062550dcd8b96a8bcb94bc1c0a8aeef

Contents?: true

Size: 1.46 KB

Versions: 24

Compression:

Stored size: 1.46 KB

Contents

# encoding: utf-8
module Mongoid

  # Contains the bahviour around inspecting documents via
  # <tt>Object#inspect</tt>.
  module Inspection

    # 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.
    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.
    #
    # @example Inspect the defined fields.
    #   document.inspect_fields
    #
    # @return [ String ] An array of pretty printed field values.
    def inspect_fields
      fields.map do |name, field|
        unless name == "_id"
          "#{name}: #{@attributes[name].inspect}"
        end
      end.compact
    end

    # Get an array of inspected dynamic fields for the document.
    #
    # @example Inspect the dynamic fields.
    #   document.inspect_dynamic_fields
    #
    # @return [ String ] An array of pretty printed dynamic field values.
    def inspect_dynamic_fields
      if Mongoid.allow_dynamic_fields
        keys = @attributes.keys - fields.keys - relations.keys - ["_id", "_type"]
        return keys.map do |name|
          "#{name}: #{@attributes[name].inspect}"
        end
      else
        []
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
mongoid-3.0.23 lib/mongoid/inspection.rb
mongoid-3.0.22 lib/mongoid/inspection.rb
mongoid-3.0.21 lib/mongoid/inspection.rb
mongoid-3.0.20 lib/mongoid/inspection.rb
mongoid-3.0.19 lib/mongoid/inspection.rb
mongoid-3.0.18 lib/mongoid/inspection.rb
mongoid-3.0.17 lib/mongoid/inspection.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.16/lib/mongoid/inspection.rb
mongoid-3.0.16 lib/mongoid/inspection.rb
mongoid-3.0.15 lib/mongoid/inspection.rb
mongoid-3.0.14 lib/mongoid/inspection.rb
mongoid-3.0.13 lib/mongoid/inspection.rb
mongoid-3.0.12 lib/mongoid/inspection.rb
mongoid-3.0.11 lib/mongoid/inspection.rb
mongoid-3.0.10 lib/mongoid/inspection.rb
mongoid-3.0.9 lib/mongoid/inspection.rb
mongoid-3.0.6 lib/mongoid/inspection.rb
mongoid-3.0.5 lib/mongoid/inspection.rb
mongoid-3.0.4 lib/mongoid/inspection.rb
mongoid-3.0.3 lib/mongoid/inspection.rb