sequel_uuid_prefix 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf00930233f9fe19eca45eaa8ee052b4f52e6fb1
4
+ data.tar.gz: 603e7de1a670e9fb1a67eec175c8736bad5b4174
5
+ SHA512:
6
+ metadata.gz: cfd7d3cd8ce10cdc9ab58489c2a51215c691a4981e339030b4b5751037a1e05129dc743151bef42a4f9cb66acaf41490854225fc7daf00c74eed5c467d3b3f76
7
+ data.tar.gz: 1bb81fe757f3ad7330d6504b4a194b943910d5fe863b11760e9ad3dc077a15683f1941b4f136baa539c8535dbdea1729f09fdb0556ad34d528ad36c736d177c8
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,34 @@
1
+ # UUID Prefix
2
+
3
+ This is a Sequel Model plugin that adds [0-9a-z] unique ID prefixed by a custom string.
4
+
5
+ The prefix isn't stored in the database. Only the Unique ID itself is.
6
+
7
+ Usage:
8
+
9
+ ```ruby
10
+ class Account < Sequel::Model
11
+ plugin Sequel::Plugins::UUIDPrefix
12
+
13
+ uuid_prefix 'a'
14
+ end
15
+
16
+ class User < Sequel::Model
17
+ plugin Sequel::Plugins::UUIDPrefix
18
+
19
+ uuid_prefix 'u'
20
+ end
21
+
22
+ # Find an account.
23
+ UUIDPrefix.find('a-xxxxxxxx')
24
+
25
+ # Find a user.
26
+ UUIDPrefix.find('u-xxxxxxxx')
27
+
28
+ user1.uuid_prefix # == 'u'
29
+ user1.canonical_uuid # == 'u-abcd1234'
30
+
31
+ Account['a-xxxxyyyy'] # Returns the account with UUID a-xxxxyyyy
32
+
33
+ Account.trim_uuid('a-abcd1234') # == 'abcd1234'
34
+ ```
@@ -0,0 +1,209 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'sequel/plugins/after_initialize'
4
+
5
+ module Sequel
6
+ module Plugins
7
+ # Sequel::Model plugin to inject the UUIDPrefix feature to the model
8
+ # class.
9
+ #
10
+ # UUIDPrefix model supports the features below:
11
+ # - UUIDPrefix.uuid_prefix to both set and get uuid_prefix for the model.
12
+ # - Collision detection for specified uuid_prefix.
13
+ # - Generate unique value for :uuid column at initialization.
14
+ # - Add column :uuid if the model is capable of :schema plugin methods.
15
+ module UUIDPrefix
16
+ UUID_TABLE='abcdefghijklmnopqrstuvwxyz0123456789'.split('').freeze
17
+ UUID_REGEX=%r/^(\w+)-([#{UUID_TABLE.join}]+)/
18
+
19
+ class InvalidUUIDError < StandardError; end
20
+ class UUIDPrefixDuplication < StandardError; end
21
+ class UUIDDuplication < StandardError; end
22
+ class UnsetUUIDPrefix < StandardError; end
23
+
24
+ def self.apply(model, _options = {})
25
+ model.plugin Sequel::Plugins::AfterInitialize
26
+ end
27
+
28
+ def self.uuid_prefix_collection
29
+ @uuid_prefix_collection ||= {}
30
+ end
31
+
32
+ # Find a model with an prefixed uuid object from the
33
+ # given canonical uuid.
34
+ #
35
+ # # Find an account.
36
+ # UUIDPrefix.find('a-xxxxxxxx')
37
+ #
38
+ # # Find a user.
39
+ # UUIDPrefix.find('u-xxxxxxxx')
40
+ def self.find(uuid)
41
+ raise ArgumentError, "Invalid uuid syntax: #{uuid}" unless uuid =~ UUID_REGEX
42
+ upc = uuid_prefix_collection[$1.downcase]
43
+ raise "Unknown uuid prefix: #{$1.downcase}" if upc.nil?
44
+ upc[:class].find(:uuid=>$2)
45
+ end
46
+
47
+ # Checks if the uuid object stored in the database.
48
+ def self.exists?(uuid)
49
+ !find(uuid).nil?
50
+ end
51
+
52
+ def self.configure(model)
53
+ model.schema_builders << proc {
54
+ unless has_column?(:uuid)
55
+ # add :uuid column with unique index constraint.
56
+ column(:uuid, String, :size=>8, :null=>false, :fixed=>true, :unique=>true)
57
+ end
58
+ }
59
+ end
60
+
61
+ module InstanceMethods
62
+ # read-only instance method to retrieve @uuid_prefix class
63
+ # variable.
64
+ def uuid_prefix
65
+ self.class.uuid_prefix
66
+ end
67
+
68
+ def before_validation
69
+ # trim uuid prefix if it is in the self[:uuid]
70
+ self[:uuid].sub!(/^#{self.class.uuid_prefix}-/, '')
71
+
72
+ super
73
+ end
74
+
75
+ def before_create
76
+ if !self.class.find(:uuid=>self[:uuid]).nil?
77
+ raise UUIDDuplication, "Duplicate UUID: #{self.canonical_uuid} already exists"
78
+ end
79
+
80
+ super
81
+ end
82
+
83
+ def after_initialize
84
+ super
85
+ # set random generated uuid value
86
+ self[:uuid] ||= Array.new(8) do UUID_TABLE[rand(UUID_TABLE.size)]; end.join
87
+ end
88
+
89
+ # Returns canonicalized uuid which has the form of
90
+ # "{uuid_prefix}-{uuid}".
91
+ def canonical_uuid
92
+ "#{self.uuid_prefix}-#{self[:uuid]}"
93
+ end
94
+ alias_method :cuuid, :canonical_uuid
95
+
96
+ def to_hash()
97
+ r = self.values.dup.merge({id: self.id,
98
+ uuid: canonical_uuid,
99
+ class_name: self.class.name.demodulize
100
+ })
101
+
102
+ serialize_columns = []
103
+
104
+ if defined? Sequel::Plugins::Serialization
105
+ if self.class.plugins.member?(Sequel::Plugins::Serialization)
106
+ self.class.deserialization_map.keys.each { |c|
107
+ serialize_columns << c
108
+ r[c] = self.__send__(c)
109
+ }
110
+ end
111
+ end
112
+
113
+ # convert Sequel::SQL::Blob column.
114
+ # TODO: look for alternative method to stop to retrieve
115
+ # db_schema hash.
116
+ self.class.db_schema.each { |c, v|
117
+ if v[:db_type] == 'text' && v[:type] == :string && !serialize_columns.member?(c)
118
+ r[c] = self.__send__(c).to_s
119
+ end
120
+ }
121
+ r
122
+ end
123
+
124
+ # generate API response document. similar to to_hash() but not
125
+ # to expose integer primary key.
126
+ def to_api_document
127
+ self.values.dup.merge({:id=>self.canonical_uuid, :uuid=>canonical_uuid})
128
+ end
129
+ end
130
+
131
+ module ClassMethods
132
+ # Getter and setter for uuid_prefix of the class.
133
+ #
134
+ # @example
135
+ # class Model1 < Sequel::Model
136
+ # plugin UUIDPrefix
137
+ # uuid_prefix('m')
138
+ # end
139
+ #
140
+ # Model1.uuid_prefix # == 'm'
141
+ # Model1.new.canonical_uuid # == 'm-abcd1234'
142
+ def uuid_prefix(prefix=nil)
143
+ if prefix
144
+ if UUIDPrefix.uuid_prefix_collection.has_key?(prefix)
145
+ raise UUIDPrefixDuplication, "Found collision for uuid_prefix key: #{prefix}"
146
+ end
147
+
148
+ UUIDPrefix.uuid_prefix_collection[prefix]={:class=>self}
149
+ @uuid_prefix = prefix
150
+ end
151
+
152
+ @uuid_prefix ||
153
+ (superclass.uuid_prefix if superclass.respond_to?(:uuid_prefix)) ||
154
+ raise(UnsetUUIDPrefix, "uuid prefix is unset for: #{self}")
155
+ end
156
+
157
+
158
+ # Override Model.[] to add lookup by uuid.
159
+ #
160
+ # @example
161
+ # Account['a-xxxxxx']
162
+ def [](*args)
163
+ if args.size == 1 and args[0].is_a? String
164
+ super(:uuid=>trim_uuid(args[0]))
165
+ else
166
+ super(*args)
167
+ end
168
+ end
169
+
170
+ # Returns dataset which has been selected for the uuid.
171
+ #
172
+ # @example
173
+ # Account.dataset_where_uuid('a-xxxxxx')
174
+ def dataset_where_uuid(p_uuid)
175
+ dataset.where(uuid: trim_uuid(p_uuid))
176
+ end
177
+
178
+ # Returns the uuid string which is removed prefix part: /^(:?\w+)-/.
179
+ #
180
+ # @example
181
+ # Account.trim_uuid('a-abcd1234') # = 'abcd1234'
182
+ # @example Will get InvalidUUIDError as the uuid with invalid prefix has been tried.
183
+ # Account.trim_uuid('u-abcd1234') # 'u-' prefix is for User model.
184
+ def trim_uuid(p_uuid)
185
+ regex = %r/^#{self.uuid_prefix}-/
186
+ if p_uuid and p_uuid =~ regex
187
+ return p_uuid.sub(regex, '')
188
+ end
189
+ raise InvalidUUIDError, "Invalid uuid or unsupported uuid: #{p_uuid} in #{self}"
190
+ end
191
+
192
+ # Checks the general uuid syntax
193
+ def check_trimmed_uuid_format(uuid)
194
+ uuid.match(/^[\w]+$/) && uuid.length <= 255
195
+ end
196
+
197
+ # Checks the uuid syntax if it is for the UUIDPrefix class.
198
+ def check_uuid_format(uuid)
199
+ uuid =~ /^#{self.uuid_prefix}-/
200
+ end
201
+
202
+ def valid_uuid_syntax?(uuid)
203
+ uuid =~ /^#{self.uuid_prefix}-[\w]+/
204
+ end
205
+ end
206
+
207
+ end
208
+ end
209
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_uuid_prefix
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Axsh Co. LTD
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: dev@axsh.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sequel/plugins/uuid_prefix.rb
20
+ - LICENSE
21
+ - README.md
22
+ homepage: https://github.com/axsh/sequel_uuid_prefix
23
+ licenses:
24
+ - LGPLv3
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Sequel Model plugin that adds [0-9a-z] unique id prefixed by a custom string.
46
+ test_files: []