sequel-enhancements 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -0
- data/lib/sequel/enhancements/version.rb +1 -1
- data/lib/sequel/plugins/uuid.rb +65 -0
- data/spec/plugins/uuid_spec.rb +58 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6f453a8797f551ebd4716ecc35e19e871436e2e
|
4
|
+
data.tar.gz: 13a9a8b7fdcff141bf0e6eadddcf60372528c37e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25514228afbdab8a4fa47957054483ce29ee00d6a7f1ccc7fe30d994b1fc1d4f6fb63cf44c564e7015e7a9c5546e02ec4bce0f8e2089cefef12431ee989edc75
|
7
|
+
data.tar.gz: 8de3003a490db354356a63c02cab2ab7f40247fc12f49864d820fbd8472bb6139b25165dfbd484d9d0bc5480714bcbb9406f508257a5e2497d4fdd5b03495b7e
|
data/.travis.yml
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require "sequel/enhancements"
|
2
|
+
require "set"
|
3
|
+
require "securerandom"
|
4
|
+
|
5
|
+
module Sequel
|
6
|
+
module Plugins
|
7
|
+
module Uuid
|
8
|
+
class ExistingModelPrefixError < Sequel::Error; end
|
9
|
+
|
10
|
+
def self.configure(model, columns, opts={})
|
11
|
+
columns = Array(columns)
|
12
|
+
set_model_prefix(opts[:prefix], model) if opts[:prefix]
|
13
|
+
model.instance_eval do
|
14
|
+
@uuid_prefix = opts[:prefix]
|
15
|
+
@uuid_fields ||= Set.new
|
16
|
+
columns.each do |c|
|
17
|
+
@uuid_fields << c
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.model_for_prefix(prefix)
|
23
|
+
Hash(@model_prefixes)[prefix]
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.set_model_prefix(prefix, model)
|
27
|
+
existing = model_for_prefix prefix
|
28
|
+
if existing
|
29
|
+
raise ExistingModelPrefixError.new("The prefix '#{prefix}' is already in use by #{existing}")
|
30
|
+
end
|
31
|
+
@model_prefixes ||= {}
|
32
|
+
@model_prefixes[prefix] = model
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
attr_reader :uuid_fields
|
37
|
+
attr_reader :uuid_prefix
|
38
|
+
end
|
39
|
+
|
40
|
+
module InstanceMethods
|
41
|
+
def _before_validation
|
42
|
+
set_uuids if new?
|
43
|
+
super
|
44
|
+
end
|
45
|
+
|
46
|
+
def set_uuids
|
47
|
+
model.uuid_fields.each do |field|
|
48
|
+
meth = :"#{field}="
|
49
|
+
send(meth, generate_uuid) if respond_to?(meth)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def generate_uuid
|
54
|
+
orig = SecureRandom.uuid
|
55
|
+
if model.uuid_prefix
|
56
|
+
model.uuid_prefix.chars.each_with_index do |c, i|
|
57
|
+
orig[i] = c
|
58
|
+
end
|
59
|
+
end
|
60
|
+
orig
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Sequel::Plugins::StringUpcaser' do
|
4
|
+
let(:db){ Sequel.mock }
|
5
|
+
let(:model_class){ Class.new(Sequel::Model(db[:test])){ @plugins.clear } }
|
6
|
+
|
7
|
+
subject { model_class.new }
|
8
|
+
|
9
|
+
before do
|
10
|
+
model_class.columns :id, :other_uuid
|
11
|
+
model_class.plugin :skip_create_refresh
|
12
|
+
model_class.plugin :uuid, [:id, :other_uuid]
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'sets uuid after save' do
|
16
|
+
subject.id.must_be_nil
|
17
|
+
subject.other_uuid.must_be_nil
|
18
|
+
subject.save
|
19
|
+
subject.id.wont_be_nil
|
20
|
+
subject.other_uuid.wont_be_nil
|
21
|
+
subject.other_uuid.wont_equal subject.id
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'sets uuid before validation' do
|
25
|
+
subject.id.must_be_nil
|
26
|
+
subject.valid?
|
27
|
+
subject.id.wont_be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'prefix' do
|
31
|
+
let(:prefix){ SecureRandom.hex(1) }
|
32
|
+
|
33
|
+
before do
|
34
|
+
Sequel::Plugins::Uuid.instance_eval{Hash(@model_prefixes).clear}
|
35
|
+
model_class.columns :id, :other_uuid
|
36
|
+
model_class.instance_eval { @plugins.clear }
|
37
|
+
model_class.plugin :uuid, :id, :prefix => prefix
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'sets prefix on uuids' do
|
41
|
+
subject.valid?
|
42
|
+
subject.id[0..1].must_equal prefix
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'allows looking up a model by prefix' do
|
46
|
+
Sequel::Plugins::Uuid.model_for_prefix(prefix).must_equal model_class
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'explodes when two models have the same prefix' do
|
50
|
+
other_model = Class.new(Sequel::Model(db[:test])){ @plugins.clear }
|
51
|
+
other_model.columns :id
|
52
|
+
other_model.instance_eval { @plugins.clear }
|
53
|
+
lambda do
|
54
|
+
other_model.plugin :uuid, :id, :prefix => prefix
|
55
|
+
end.must_raise Sequel::Plugins::Uuid::ExistingModelPrefixError
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel-enhancements
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- peer60
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,6 +88,7 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
91
92
|
- Gemfile
|
92
93
|
- LICENSE.txt
|
93
94
|
- README.md
|
@@ -100,6 +101,7 @@ files:
|
|
100
101
|
- lib/sequel/plugins/string_downcaser.rb
|
101
102
|
- lib/sequel/plugins/string_nilifier.rb
|
102
103
|
- lib/sequel/plugins/string_upcaser.rb
|
104
|
+
- lib/sequel/plugins/uuid.rb
|
103
105
|
- sequel-enhancements.gemspec
|
104
106
|
- spec/extensions/sqlite_json_spec.rb
|
105
107
|
- spec/plugins/hash_cleaner_spec.rb
|
@@ -107,6 +109,7 @@ files:
|
|
107
109
|
- spec/plugins/string_downcaser_spec.rb
|
108
110
|
- spec/plugins/string_nilifier_spec.rb
|
109
111
|
- spec/plugins/string_upcaser_spec.rb
|
112
|
+
- spec/plugins/uuid_spec.rb
|
110
113
|
- spec/spec_helper.rb
|
111
114
|
homepage: https://bitbucket.org/peer60/sequel-enhancements
|
112
115
|
licenses:
|
@@ -139,4 +142,5 @@ test_files:
|
|
139
142
|
- spec/plugins/string_downcaser_spec.rb
|
140
143
|
- spec/plugins/string_nilifier_spec.rb
|
141
144
|
- spec/plugins/string_upcaser_spec.rb
|
145
|
+
- spec/plugins/uuid_spec.rb
|
142
146
|
- spec/spec_helper.rb
|