mm_uses_uuid 0.0.13 → 0.0.14
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.
- data/lib/mm_uses_uuid.rb +39 -18
- data/lib/mm_uses_uuid/version.rb +1 -1
- data/spec/mm_uses_uuid_spec.rb +30 -6
- metadata +5 -25
data/lib/mm_uses_uuid.rb
CHANGED
@@ -36,21 +36,42 @@ class UuidModel
|
|
36
36
|
|
37
37
|
include MongoMapper::Document
|
38
38
|
|
39
|
-
@@
|
39
|
+
@@lsn_class_lookup ||= {}
|
40
40
|
|
41
|
-
def self.
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
def self.add_lsn_mapping(ind, klass)
|
42
|
+
@@lsn_class_lookup[ind] = klass
|
43
|
+
@@class_lsn_lookup = @@lsn_class_lookup.invert
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.lsn_class_lookup
|
47
|
+
@@lsn_class_lookup
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.class_lsn_lookup
|
51
|
+
@@class_lsn_lookup
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.find(*ids)
|
55
|
+
ids.flatten!
|
56
|
+
ids_by_class = ids.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |id, hsh|
|
46
57
|
lsn = id.to_s[-1].hex
|
47
|
-
klass = @@
|
48
|
-
|
49
|
-
|
50
|
-
|
58
|
+
klass = @@lsn_class_lookup[lsn]
|
59
|
+
if klass.nil?
|
60
|
+
raise "expected to find a class in @@lsn_class_lookup[#{lsn}] of the MongoMapper module but there was no entry. You need to set uuid_lsn in your class."
|
61
|
+
end
|
62
|
+
hsh[klass] << id
|
51
63
|
end
|
52
64
|
result = ids_by_class.map {|klass, ids| klass.find(ids)} .flatten
|
53
|
-
|
65
|
+
ids.length == 1 ? result.first : result
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.find!(*ids)
|
69
|
+
raise MongoMapper::DocumentNotFound, "Couldn't find without an ID" if ids.size == 0
|
70
|
+
find(*ids).tap do |result|
|
71
|
+
if result.nil? || ids.size != Array(result).size
|
72
|
+
raise MongoMapper::DocumentNotFound, "Couldn't find all of the ids (#{ids.join(',')}). Found #{Array(result).size}, but was expecting #{ids.size}"
|
73
|
+
end
|
74
|
+
end
|
54
75
|
end
|
55
76
|
|
56
77
|
end
|
@@ -96,13 +117,9 @@ module MmUsesUuid
|
|
96
117
|
end
|
97
118
|
|
98
119
|
def uuid_lsn(lsn_integer)
|
99
|
-
|
120
|
+
UuidModel.add_lsn_mapping(lsn_integer, self)
|
100
121
|
end
|
101
122
|
|
102
|
-
def add_class_lsn(klass, lsn_integer)
|
103
|
-
UuidModel.class_eval "@@lsn_class[#{lsn_integer}] = #{klass}"
|
104
|
-
end
|
105
|
-
|
106
123
|
end
|
107
124
|
|
108
125
|
|
@@ -135,8 +152,12 @@ module MmUsesUuid
|
|
135
152
|
|
136
153
|
def make_uuid
|
137
154
|
uuid = SecureRandom.uuid.gsub!('-', '')
|
138
|
-
|
139
|
-
|
155
|
+
if self.class.single_collection_inherited?
|
156
|
+
lookup_class = self.class.collection_name.singularize.camelize.constantize
|
157
|
+
else
|
158
|
+
lookup_class = self.class
|
159
|
+
end
|
160
|
+
if replacement_lsn = UuidModel.class_lsn_lookup[lookup_class]
|
140
161
|
uuid[-1] = replacement_lsn.to_s(16)
|
141
162
|
end
|
142
163
|
bson_encoded_uuid = BSON::Binary.new(uuid, BSON::Binary::SUBTYPE_UUID)
|
data/lib/mm_uses_uuid/version.rb
CHANGED
data/spec/mm_uses_uuid_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require File.expand_path('../../lib/mm_uses_uuid', __FILE__)
|
2
|
+
require 'ruby-debug'
|
2
3
|
|
3
4
|
describe MmUsesUuid do
|
4
5
|
|
@@ -8,6 +9,7 @@ describe MmUsesUuid do
|
|
8
9
|
plugin MmUsesUuid
|
9
10
|
|
10
11
|
key :name, String
|
12
|
+
belongs_to :owner, :class_name => 'UuidModel', :required => true
|
11
13
|
many :people, :class_name => 'Person'
|
12
14
|
|
13
15
|
uuid_lsn 0
|
@@ -38,19 +40,25 @@ describe MmUsesUuid do
|
|
38
40
|
before(:each) do
|
39
41
|
#we have to use a before(:each) to get the stubbing to work :(
|
40
42
|
SecureRandom.stub!(:uuid).and_return(
|
41
|
-
"11111111-1111-4111-y111-111111111111",
|
42
43
|
"22222222-2222-4222-y222-222222222222",
|
44
|
+
"11111111-1111-4111-y111-111111111111",
|
43
45
|
"11111111-1111-4111-y111-111111111111", #we repeat these so that our safe uuid creation tests will detect a collision and be forced to search
|
44
46
|
"11111111-1111-4111-y111-111111111111",
|
45
47
|
"11111111-1111-4111-y111-111111111111",
|
46
48
|
"33333333-3333-4333-y333-333333333333"
|
47
49
|
)
|
48
|
-
@group = Group.create(name: 'mongo_mapper fanclub')
|
49
50
|
|
50
51
|
@person = Person.new(name: 'Jon', age: 33)
|
52
|
+
@person.save
|
53
|
+
|
54
|
+
@group = Group.new(name: 'mongo_mapper fanclub')
|
55
|
+
@group.owner = @person
|
56
|
+
@group.save
|
57
|
+
|
51
58
|
@person.interests << @group
|
52
59
|
@person.interests << @person #I'm very self-centred
|
53
60
|
@person.save
|
61
|
+
|
54
62
|
end
|
55
63
|
|
56
64
|
it "should cause newly initialized objects to have a BSON::Binary uuid" do
|
@@ -103,16 +111,32 @@ describe MmUsesUuid do
|
|
103
111
|
|
104
112
|
context 'finding by uuid' do
|
105
113
|
|
106
|
-
it "with a BSON::Binary uuid" do
|
107
|
-
found_group =
|
114
|
+
it "should find with a BSON::Binary uuid" do
|
115
|
+
found_group = UuidModel.find(@group._id)
|
108
116
|
found_group._id.should == @group._id
|
109
117
|
end
|
110
118
|
|
111
|
-
it "with a String uuid" do
|
112
|
-
found_group =
|
119
|
+
it "should find with a String uuid" do
|
120
|
+
found_group = UuidModel.find(@group._id.to_s)
|
113
121
|
found_group._id.should == @group._id
|
114
122
|
end
|
115
123
|
|
124
|
+
it "find! should raise an error if an id is not found" do
|
125
|
+
lambda { UuidModel.find! "ffffffffffff4fffyffffffffffffff0" }.should raise_error(MongoMapper::DocumentNotFound)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "find! should raise an error if one of many ids is not found" do
|
129
|
+
lambda { UuidModel.find! "ffffffffffff4fffyffffffffffffff0", @group._id }.should raise_error(MongoMapper::DocumentNotFound)
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
context "finding indirectly via belongs_to assoc" do
|
135
|
+
|
136
|
+
it "should find associated objects" do
|
137
|
+
@group.owner.should == @person
|
138
|
+
end
|
139
|
+
|
116
140
|
end
|
117
141
|
|
118
142
|
after(:each) do
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mm_uses_uuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 13
|
9
|
-
version: 0.0.13
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.14
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Jonathan Chambers
|
@@ -14,8 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2012-
|
18
|
-
default_executable:
|
13
|
+
date: 2012-07-22 00:00:00 Z
|
19
14
|
dependencies:
|
20
15
|
- !ruby/object:Gem::Dependency
|
21
16
|
name: rspec
|
@@ -25,9 +20,6 @@ dependencies:
|
|
25
20
|
requirements:
|
26
21
|
- - ">="
|
27
22
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 2
|
30
|
-
- 7
|
31
23
|
version: "2.7"
|
32
24
|
type: :development
|
33
25
|
version_requirements: *id001
|
@@ -39,10 +31,6 @@ dependencies:
|
|
39
31
|
requirements:
|
40
32
|
- - ">="
|
41
33
|
- !ruby/object:Gem::Version
|
42
|
-
segments:
|
43
|
-
- 1
|
44
|
-
- 5
|
45
|
-
- 0
|
46
34
|
version: 1.5.0
|
47
35
|
type: :development
|
48
36
|
version_requirements: *id002
|
@@ -54,10 +42,6 @@ dependencies:
|
|
54
42
|
requirements:
|
55
43
|
- - ">="
|
56
44
|
- !ruby/object:Gem::Version
|
57
|
-
segments:
|
58
|
-
- 0
|
59
|
-
- 10
|
60
|
-
- 1
|
61
45
|
version: 0.10.1
|
62
46
|
type: :runtime
|
63
47
|
version_requirements: *id003
|
@@ -81,7 +65,6 @@ files:
|
|
81
65
|
- lib/mm_uses_uuid/version.rb
|
82
66
|
- mm_uses_uuid.gemspec
|
83
67
|
- spec/mm_uses_uuid_spec.rb
|
84
|
-
has_rdoc: true
|
85
68
|
homepage: https://github.com/jmchambers/mm_uses_uuid
|
86
69
|
licenses:
|
87
70
|
- MIT
|
@@ -95,23 +78,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
95
78
|
requirements:
|
96
79
|
- - ">="
|
97
80
|
- !ruby/object:Gem::Version
|
98
|
-
segments:
|
99
|
-
- 0
|
100
81
|
version: "0"
|
101
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
83
|
none: false
|
103
84
|
requirements:
|
104
85
|
- - ">="
|
105
86
|
- !ruby/object:Gem::Version
|
106
|
-
segments:
|
107
|
-
- 0
|
108
87
|
version: "0"
|
109
88
|
requirements: []
|
110
89
|
|
111
90
|
rubyforge_project:
|
112
|
-
rubygems_version: 1.
|
91
|
+
rubygems_version: 1.8.17
|
113
92
|
signing_key:
|
114
93
|
specification_version: 3
|
115
94
|
summary: UUIDs for MM
|
116
95
|
test_files: []
|
117
96
|
|
97
|
+
has_rdoc:
|