-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add inspect_pk plugin to make it easier to retrieve model instance ba…
…sed on inspect output
- Loading branch information
1 parent
d20c40d
commit 0bda470
Showing
5 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require_relative "spec_helper" | ||
|
||
describe "inspect_pk plugin" do | ||
def inspect(vals={}, &block) | ||
Class.new(Sequel::Model) do | ||
def self.name; 'M' end | ||
plugin :inspect_pk | ||
set_dataset DB.dataset | ||
class_exec(&block) if block | ||
columns(*vals.keys) | ||
unrestrict_primary_key | ||
end.new(vals).inspect | ||
end | ||
|
||
it "should not include primary key value if model does not have primary key" do | ||
inspect(id: 1){no_primary_key}.must_equal "#<M @values=#{{:id=>1}.inspect}>" | ||
end | ||
|
||
it "should not include primary key value if model has scalar primary key and instance does not have primary key value" do | ||
inspect{set_primary_key :id}.must_equal "#<M @values={}>" | ||
end | ||
|
||
it "should not include primary key value if model instance has composite primary key and instance does not have values for all primary key components" do | ||
[{id1: 1, id2: nil}, {id1: nil, id2: 2}, {id1: nil, id2: nil}].each do |vals| | ||
inspect(vals){set_primary_key [:id1, :id2]}.must_equal "#<M @values=#{vals.inspect}>" | ||
end | ||
end | ||
|
||
it "should include primary value for scalar primary key if present" do | ||
inspect(id: 1){set_primary_key :id}.must_equal "#<M[1] @values=#{{id: 1}.inspect}>" | ||
end | ||
|
||
it "should include primary value for composite primary key if all fields present" do | ||
vals = {id1: 1, id2: 2} | ||
inspect(vals){set_primary_key [:id1, :id2]}.must_equal "#<M[[1, 2]] @values=#{vals.inspect}>" | ||
end | ||
|
||
it "should use inspect value of primary key" do | ||
inspect(id: "1"){}.must_equal "#<M[\"1\"] @values=#{{id: "1"}.inspect}>" | ||
end | ||
|
||
it "should use inspect_pk method to get inspect pk value" do | ||
inspect{def inspect_pk; "2" end}.must_equal "#<M[\"2\"] @values={}>" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters