File: inspect_pk.rb

package info (click to toggle)
ruby-sequel 5.101.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,312 kB
  • sloc: ruby: 124,594; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,226 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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