Sha256: 025e9c6b7b07d433c0e293dcc845d81f8c43f808eb9f9f0db21b0751ee089daa

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen-string-literal: true

module Sequel
  module Plugins
    # The inspect_pk plugin includes the pk right next to the
    # model name in inspect, allowing for easily copying and
    # pasting to retrieve a copy of the object:
    #
    #   Album.with_pk(1).inspect
    #   #         default: #<Album @values={...}>
    #   # with inspect_pk: #<Album[1] @values={...}>
    #
    # Usage:
    #
    #   # Make all model instances include pk in inspect output
    #   Sequel::Model.plugin :inspect_pk
    #
    #   # Make Album instances include pk in inspect output
    #   Album.plugin :inspect_pk
    module InspectPk
      module InstanceMethods
        private

        # The primary key value to include in the inspect output, if any.
        # For composite primary keys, this only includes a value if all
        # fields are present.
        def inspect_pk
          if primary_key && (pk = self.pk) && (!(Array === pk) || pk.all?)
            pk
          end
        end

        # Include the instance's primary key in the output.
        def inspect_prefix
          if v = inspect_pk
            "#{super}[#{v.inspect}]"
          else
            super
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sequel-5.87.0 lib/sequel/plugins/inspect_pk.rb