Module: Sequel::Plugins::UUIDPrefix
- Defined in:
- lib/sequel/plugins/uuid_prefix.rb
Overview
Sequel::Model plugin to inject the UUIDPrefix feature to the model class.
UUIDPrefix model supports the features below:
-
UUIDPrefix.uuid_prefix to both set and get uuid_prefix for the model.
-
Collision detection for specified uuid_prefix.
-
Generate unique value for :uuid column at initialization.
-
Add column :uuid if the model is capable of :schema plugin methods.
Defined Under Namespace
Modules: ClassMethods, InstanceMethods Classes: InvalidUUIDError, UUIDDuplication, UUIDPrefixDuplication, UnsetUUIDPrefix
Constant Summary collapse
- UUID_TABLE =
'abcdefghijklmnopqrstuvwxyz0123456789'.split('').freeze
- UUID_REGEX =
%r/^(\w+)-([#{UUID_TABLE.join}]+)/
Class Method Summary collapse
- .apply(model, _options = {}) ⇒ Object
- .configure(model) ⇒ Object
-
.exists?(uuid) ⇒ Boolean
Checks if the uuid object stored in the database.
-
.find(uuid) ⇒ Object
Find a model with an prefixed uuid object from the given canonical uuid.
- .uuid_prefix_collection ⇒ Object
Class Method Details
.apply(model, _options = {}) ⇒ Object
24 25 26 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 24 def self.apply(model, = {}) model.plugin Sequel::Plugins::AfterInitialize end |
.configure(model) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 52 def self.configure(model) model.schema_builders << proc { unless has_column?(:uuid) # add :uuid column with unique index constraint. column(:uuid, String, :size=>8, :null=>false, :fixed=>true, :unique=>true) end } end |
.exists?(uuid) ⇒ Boolean
Checks if the uuid object stored in the database.
48 49 50 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 48 def self.exists?(uuid) !find(uuid).nil? end |
.find(uuid) ⇒ Object
Find a model with an prefixed uuid object from the given canonical uuid.
# Find an account. UUIDPrefix.find(‘a-xxxxxxxx’)
# Find a user. UUIDPrefix.find(‘u-xxxxxxxx’)
40 41 42 43 44 45 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 40 def self.find(uuid) raise ArgumentError, "Invalid uuid syntax: #{uuid}" unless uuid =~ UUID_REGEX upc = uuid_prefix_collection[$1.downcase] raise "Unknown uuid prefix: #{$1.downcase}" if upc.nil? upc[:class].find(:uuid=>$2) end |
.uuid_prefix_collection ⇒ Object
28 29 30 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 28 def self.uuid_prefix_collection @uuid_prefix_collection ||= {} end |