friendly_uuid 0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/friendly_uuid.rb +72 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 98e988bb5dfd439d830209d5b1faf70835abe26c7f5c0c1e5378d8951401c75f
|
4
|
+
data.tar.gz: f5093ac244b08c647dcb2711f89cdf34c116176dae26914903fe1be287c6e086
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 294ccf76035580dcc454f3b3b5dbaf3edf3c37a1a468f7f7fed9bc08d9a65c64424f9e8a33ade79c9ef80111eb2179e94073e3117256776827eebeb228817cc3
|
7
|
+
data.tar.gz: 14ed6bc1d2ab87d4df1e3428a4e89eb16fc6d8cf9660d44a5be6cdee19c5e0f9e1c3f784f79f8839e22a54b84fd6104706075cf1cf2a76ca6f6c742e80b7990a
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module FriendlyUUID
|
2
|
+
def self.extended(model_class)
|
3
|
+
model_class.class_eval do
|
4
|
+
include Base
|
5
|
+
extend Class
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.included(model_class)
|
10
|
+
model_class.class_eval do
|
11
|
+
include Base
|
12
|
+
extend Class
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Base
|
17
|
+
def to_param
|
18
|
+
self.class.compact(self.id)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module Class
|
23
|
+
def find(id)
|
24
|
+
super(self.expand(id))
|
25
|
+
end
|
26
|
+
|
27
|
+
def expand(short_uuid)
|
28
|
+
self.expand_to_record(short_uuid).id
|
29
|
+
end
|
30
|
+
|
31
|
+
def compact(uuid)
|
32
|
+
(0..uuid.length).each do |length|
|
33
|
+
candidate = uuid[0..length]
|
34
|
+
return candidate if self.expand(candidate) == uuid
|
35
|
+
end
|
36
|
+
raise ValueError("Cannot find expansion for UUID #{uuid}")
|
37
|
+
end
|
38
|
+
|
39
|
+
def expand_to_record(short_uuid)
|
40
|
+
raise ActiveRecord::RecordNotFound unless short_uuid
|
41
|
+
record = self.possible_expansions(short_uuid).first
|
42
|
+
raise ActiveRecord::RecordNotFound unless record
|
43
|
+
record
|
44
|
+
end
|
45
|
+
|
46
|
+
def possible_expansions(short_uuid)
|
47
|
+
self.where(substr_query, short_uuid.length, short_uuid).order(created_at: :asc)
|
48
|
+
end
|
49
|
+
|
50
|
+
def find_by(arg, *args)
|
51
|
+
if arg.respond_to?(:[])
|
52
|
+
arg[:id] = self.expand(arg[:id]) if arg[:id]
|
53
|
+
arg["id"] = self.expand(arg["id"]) if arg["id"]
|
54
|
+
end
|
55
|
+
|
56
|
+
super
|
57
|
+
end
|
58
|
+
|
59
|
+
def substr_query
|
60
|
+
case self.connection_config[:adapter]
|
61
|
+
when "mysql2"
|
62
|
+
raise ValueError("Sorry, FriendlyUUID does not support MySQL")
|
63
|
+
when "postgresql"
|
64
|
+
"LEFT(#{self.table_name}.id::text, ?) = ?"
|
65
|
+
when "sqlite3"
|
66
|
+
"SUBSTR(#{self.table_name}?.id, 0, ?) = ?"
|
67
|
+
else
|
68
|
+
raise ValueError("Unknown database type; FriendlyUUID cannot support it")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: friendly_uuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Carlsson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-10-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: FriendlyUUID shortens every UUID to have only as many characters as it
|
14
|
+
needs to be unique.
|
15
|
+
email: qhiiyr@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/friendly_uuid.rb
|
21
|
+
homepage: https://github.com/glacials/friendly_uuid
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.1.4
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Make UUIDs pretty enough for use in URLs
|
44
|
+
test_files: []
|