friendly_uuid 0.1.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/friendly_uuid.rb +72 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 574a40d21c18aea88a5c97e387daf0a81d3c4629dc0193e3d433f5eb35433358
4
+ data.tar.gz: e01805a89fc17df9ebffe0a4db2e5fab7e6eeeb9b13d718b6fad3c870032b5e2
5
+ SHA512:
6
+ metadata.gz: 60812d58683d64b2b0d9d8f419c194c5fef7be8ddb80a1fdde3bf9a583f04bd314cc70e06f8aac0fba139bff0407d32a04e304b92097d89ae772dd3b66c3297b
7
+ data.tar.gz: d8d82690ca67437bb63d7dd6b2e5c45d29f88e71695e4f0181893086881717834927be707ee917a928c83535268350c0733c02fd9939b73a87beebe6b8dcc239
@@ -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.1.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: []