mongoid-uuid 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/mongoid/uuid/uuid.rb +25 -0
- data/lib/mongoid/uuid/validator.rb +15 -0
- data/lib/mongoid/uuid/version.rb +5 -0
- data/lib/mongoid/uuid.rb +21 -0
- data/spec/model.rb +6 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/uuid_spec.rb +48 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 37af931d39282a2cb01a92da118e0fec51368282
|
4
|
+
data.tar.gz: 257efd5fd91a2aa0f99fbb726022af6e5a0ea1d3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 48d886539fbd2ef375c0968beb697dac8b00860f372f48c545434f9578f13ee16cfafb906416204dc26d5bd31c9c3756e18ceed7185595b01420ee695fea3df7
|
7
|
+
data.tar.gz: 8c3ebe248947e5ffebeed505498a56367b50966e61155b67c1a28b62a239d4852a76feb9ce1e2f5cec5b3ddce89ff6e4cbdd3fb33de59bf41460b644842908cc
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid # :nodoc:
|
3
|
+
module Uuid #:nodoc:
|
4
|
+
module Uuid
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
field :uuid
|
9
|
+
index({:uuid => 1}, {:unique => true})
|
10
|
+
before_validation :generate_uuid
|
11
|
+
validates :uuid, :uniqueness => true, :uuid => false
|
12
|
+
attr_readonly :uuid
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
# Sets unique UUID unless uuid is present.
|
18
|
+
def generate_uuid
|
19
|
+
if [nil, ""].include?(uuid)
|
20
|
+
self.uuid = UUID.generate
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Uuid
|
3
|
+
module Validator
|
4
|
+
class UuidValidator < ActiveModel::EachValidator
|
5
|
+
|
6
|
+
# Ensures that every value is of a valid compact UUID format.
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
unless Mongoid::Uuid.validate(value)
|
9
|
+
record.errors.add(attribute, :invalid_uuid)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/mongoid/uuid.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "uuid"
|
3
|
+
require "mongoid"
|
4
|
+
require "mongoid/uuid/version"
|
5
|
+
require "mongoid/uuid/uuid"
|
6
|
+
require "mongoid/uuid/validator"
|
7
|
+
|
8
|
+
module Mongoid
|
9
|
+
module Uuid
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
include Uuid
|
12
|
+
|
13
|
+
def self.validate(uuid)
|
14
|
+
UUID.validate(uuid)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
ActiveModel::Validations.send(:include, Mongoid::Uuid::Validator)
|
data/spec/model.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'mongoid'
|
5
|
+
require 'mongoid/uuid'
|
6
|
+
|
7
|
+
require 'rspec'
|
8
|
+
|
9
|
+
if ENV["CI"]
|
10
|
+
require "simplecov"
|
11
|
+
require "coveralls"
|
12
|
+
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
13
|
+
SimpleCov.start do
|
14
|
+
add_filter "spec"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Mongoid.configure do |config|
|
19
|
+
config.connect_to('mongoid_uuid_test')
|
20
|
+
end
|
21
|
+
|
22
|
+
require "model"
|
23
|
+
|
24
|
+
RSpec.configure do |config|
|
25
|
+
config.expect_with :rspec do |c|
|
26
|
+
c.syntax = :expect
|
27
|
+
end
|
28
|
+
config.include RSpec::Matchers
|
29
|
+
config.include Mongoid::Matchers
|
30
|
+
config.mock_with :rspec
|
31
|
+
config.after :all do
|
32
|
+
Mongoid::Config.purge!
|
33
|
+
end
|
34
|
+
end
|
data/spec/uuid_spec.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
require "uuid"
|
4
|
+
|
5
|
+
describe Mongoid::Uuid do
|
6
|
+
describe "Create with uuid" do
|
7
|
+
it "create with default uuid" do
|
8
|
+
item = ItemWithUuid.create!(name: 'foo')
|
9
|
+
expect(item.uuid.length).to eq(36)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "create with generated uuid" do
|
13
|
+
uuid = UUID.generate;
|
14
|
+
item = ItemWithUuid.create!(name: 'foo', uuid: uuid)
|
15
|
+
expect(item.uuid).to eq(uuid)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "create with invalid uuid exception" do
|
19
|
+
invalid_uuid = 'baz-baz'
|
20
|
+
expect {
|
21
|
+
|
22
|
+
ItemWithUuid.create!(name: 'foo', uuid: invalid_uuid)
|
23
|
+
|
24
|
+
}.to raise_error(Mongoid::Errors::Validations)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "create with invalid uuid" do
|
28
|
+
invalid_uuid = 'baz-baz'
|
29
|
+
expect(ItemWithUuid.create(name: 'foo', uuid: invalid_uuid).valid?).to eq(false)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "Test read only uuid" do
|
35
|
+
it "attempt to remove the uuid" do
|
36
|
+
expect {
|
37
|
+
ItemWithUuid.create!(name: 'foo').remove_attribute(:uuid)
|
38
|
+
}.to raise_error(Mongoid::Errors::ReadonlyAttribute)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "attempt to update the uuid" do
|
42
|
+
expect {
|
43
|
+
ItemWithUuid.create!(name: 'foo').update_attribute(:uuid, 'baz')
|
44
|
+
}.to raise_error(Mongoid::Errors::ReadonlyAttribute)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-uuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuri Druzkov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: uuid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.3.7
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.3.7
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mongoid
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - <=
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '3.0'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - <=
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '4.0'
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.9.2
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 0.9.2
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '2.14'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '2.14'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: yard
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ~>
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0.8'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ~>
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0.8'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: mongoid-rspec
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '1.9'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '1.9'
|
103
|
+
description: ''
|
104
|
+
email:
|
105
|
+
- bad1lamer@gmail.com
|
106
|
+
executables: []
|
107
|
+
extensions: []
|
108
|
+
extra_rdoc_files: []
|
109
|
+
files:
|
110
|
+
- lib/mongoid/uuid.rb
|
111
|
+
- lib/mongoid/uuid/validator.rb
|
112
|
+
- lib/mongoid/uuid/uuid.rb
|
113
|
+
- lib/mongoid/uuid/version.rb
|
114
|
+
- spec/model.rb
|
115
|
+
- spec/uuid_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
homepage: http://github.com/badlamer/mongoid-uuid
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '1.9'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: 1.3.6
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project: mongoid-uuid
|
137
|
+
rubygems_version: 2.0.3
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Uuid fields for mongoid
|
141
|
+
test_files:
|
142
|
+
- spec/model.rb
|
143
|
+
- spec/uuid_spec.rb
|
144
|
+
- spec/spec_helper.rb
|
145
|
+
has_rdoc:
|