mongoid-uuid-securerandom 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 09677fa829a6705e6ee80d659e1feb4de45ec1eb
4
+ data.tar.gz: 520472075c10c2cb5c95f1cecdc0c7cacb3396d4
5
+ SHA512:
6
+ metadata.gz: e335283b96e8634dfefac907e540cd5112bb62059c3964376d39182363ec7e4c91754591e1c35dac197ac2d71be56832a44adb0d7864b74e91982af8786550e6
7
+ data.tar.gz: 3579ccaf8207dd549c78ea77f4e2e34a807c3a098bb6a79a612ffeaaa434b2cbf477d9e69c40cc8638ccc030a6ca2a6d9c2ca2c3f6c4acf83a1d12cba2b269a3
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ require "mongoid"
3
+ require "mongoid/uuid/version"
4
+ require "mongoid/uuid/uuid"
5
+ require "mongoid/uuid/validator"
6
+
7
+ module Mongoid
8
+ module Uuid
9
+ extend ActiveSupport::Concern
10
+ include Uuid
11
+
12
+ def self.validate(uuid)
13
+ uuid.class.to_s == 'BSON::Binary' and
14
+ uuid.type == :uuid and
15
+ validate_format(uuid.data)
16
+ end
17
+
18
+ # https://github.com/assaf/uuid/blob/27ae70845ff9f3be71060f473565aa7d1e6ed781/lib/uuid.rb#L199
19
+ def self.validate_format(uuid)
20
+ return true if uuid =~ /\A(urn:uuid:)?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i
21
+ end
22
+ end
23
+ end
24
+
25
+
26
+ ActiveModel::Validations.send(:include, Mongoid::Uuid::Validator)
@@ -0,0 +1,26 @@
1
+ require 'bson'
2
+ require 'moped'
3
+
4
+ # encoding: utf-8
5
+ module Mongoid # :nodoc:
6
+ module Uuid #:nodoc:
7
+ module Uuid
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ field :uuid, type: BSON::Binary, default: -> { BSON::Binary.new(SecureRandom.uuid, :uuid) }
12
+ index({:uuid => 1}, {:unique => true})
13
+ before_validation :generate_uuid
14
+ validates :uuid, :uniqueness => true, :uuid => true
15
+ attr_readonly :uuid
16
+ end
17
+
18
+ private
19
+
20
+ # Sets unique UUID unless uuid is present.
21
+ def generate_uuid
22
+ self.uuid = BSON::Binary.new(SecureRandom.uuid, :uuid) if uuid.to_s.empty?
23
+ end
24
+ end
25
+ end
26
+ 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
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Uuid
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class ItemWithUuid
2
+ include Mongoid::Document
3
+ include Mongoid::Uuid
4
+
5
+ field :name
6
+ end
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'mongoid'
5
+ require 'mongoid/uuid'
6
+ require 'mongoid-rspec'
7
+
8
+ require 'rspec'
9
+
10
+ if ENV["CI"]
11
+ require "simplecov"
12
+ require "coveralls"
13
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
14
+ SimpleCov.start do
15
+ add_filter "spec"
16
+ end
17
+ end
18
+
19
+ Mongoid.configure do |config|
20
+ config.connect_to('mongoid_uuid_test')
21
+ end
22
+
23
+ require 'model'
24
+
25
+ RSpec.configure do |config|
26
+ config.expect_with :rspec do |c|
27
+ c.syntax = :expect
28
+ end
29
+ config.include RSpec::Matchers
30
+ config.include Mongoid::Matchers
31
+ config.mock_with :rspec
32
+ config.after :all do
33
+ Mongoid::Config.purge!
34
+ end
35
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Uuid do
4
+ describe "Create with uuid" do
5
+ it "create with default uuid" do
6
+ item = ItemWithUuid.create!(name: 'foo')
7
+ expect(item.uuid.data.length).to eq(36)
8
+ end
9
+
10
+ it "create with generated uuid" do
11
+ uuid = BSON::Binary.new(SecureRandom.uuid, :uuid)
12
+ item = ItemWithUuid.create!(name: 'foo', uuid: uuid)
13
+ expect(item.uuid).to eq(uuid)
14
+ end
15
+
16
+ it "create with uuid with wrong type" do
17
+ uuid = SecureRandom.uuid
18
+ item = ItemWithUuid.create(name: 'foo', uuid: uuid)
19
+ expect(item.valid?).to eq(false)
20
+ end
21
+
22
+ it "create with invalid uuid exception" do
23
+ invalid_uuid = 'baz-baz1'
24
+ expect {
25
+ ItemWithUuid.create!(name: 'foo', uuid: invalid_uuid)
26
+ }.to raise_error(Mongoid::Errors::Validations)
27
+ end
28
+
29
+ it "create with invalid uuid" do
30
+ invalid_uuid = 'baz-baz2'
31
+ expect(ItemWithUuid.create(name: 'foo', uuid: invalid_uuid).valid?).to eq(false)
32
+ end
33
+
34
+ end
35
+
36
+ describe "Test read only uuid" do
37
+ it "attempt to remove the uuid" do
38
+ expect {
39
+ ItemWithUuid.create!(name: 'foo').remove_attribute(:uuid)
40
+ }.to raise_error(Mongoid::Errors::ReadonlyAttribute)
41
+ end
42
+
43
+ it "attempt to update the uuid" do
44
+ expect {
45
+ ItemWithUuid.create!(name: 'foo').update_attribute(:uuid, 'baz')
46
+ }.to raise_error(Mongoid::Errors::ReadonlyAttribute)
47
+ end
48
+ end
49
+
50
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-uuid-securerandom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Warren Guy
8
+ - Yury Druzkov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-07-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yard
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: mongoid
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: mongoid-rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 2.0.0.rc1
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 2.0.0.rc1
84
+ description: ''
85
+ email:
86
+ - warren@guy.net.au
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - lib/mongoid/uuid.rb
92
+ - lib/mongoid/uuid/uuid.rb
93
+ - lib/mongoid/uuid/validator.rb
94
+ - lib/mongoid/uuid/version.rb
95
+ - spec/model.rb
96
+ - spec/spec_helper.rb
97
+ - spec/uuid_spec.rb
98
+ homepage: http://github.com/warrenguy/mongoid-uuid-securerandom
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '1.9'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: 1.3.6
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.2.2
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Random Uuid fields for mongoid
122
+ test_files:
123
+ - spec/spec_helper.rb
124
+ - spec/model.rb
125
+ - spec/uuid_spec.rb