mm_uses_uuid 0.0.6 → 0.0.8
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/README.md +27 -6
- data/lib/mm_uses_uuid/version.rb +1 -1
- data/lib/mm_uses_uuid.rb +20 -5
- data/spec/mm_uses_uuid_spec.rb +14 -2
- metadata +2 -2
data/README.md
CHANGED
@@ -61,15 +61,36 @@ class Group
|
|
61
61
|
|
62
62
|
many :people, :class_name => 'Person'
|
63
63
|
|
64
|
-
uuid_lsn
|
64
|
+
uuid_lsn 0x0
|
65
65
|
end
|
66
66
|
```
|
67
67
|
|
68
|
-
Once this value is set you can use `
|
69
|
-
the last character of the UUIDs you pass. So for the example above, all UUIDs generated for new Group objects will end in '
|
70
|
-
and, if you pass a UUID ending in '
|
68
|
+
Once this value is set you can use `UuidModel.find(...)` to find by id (or a list of ids) and it will automatically detect the class by inspecting
|
69
|
+
the last character of the UUIDs you pass. So for the example above, all UUIDs generated for new Group objects will end in '0'
|
70
|
+
and, if you pass a UUID ending in '0' to `UuidModel.find`, it will pass that request on to `Group.find()`. Clearly, this method only allows you
|
71
|
+
to target 16 collections at most as the last nibble can only have values of 0 to 15.
|
72
|
+
|
73
|
+
This method can be useful if you need to implement a polymorphic many-to-many association but you don't want to use [Single Collection Inheritance][1]
|
74
|
+
because the polymorphic values have significantly different behaviours and attributes. The following example shows how to do this:
|
71
75
|
|
72
|
-
|
73
|
-
|
76
|
+
```
|
77
|
+
class Person
|
78
|
+
include MongoMapper::Document
|
79
|
+
plugin MmUsesUuid
|
80
|
+
|
81
|
+
key :name
|
82
|
+
key :age
|
83
|
+
|
84
|
+
key :interest_ids, Array
|
85
|
+
many :interests, :in => :interest_ids, :class_name => 'UuidModel' #values can be a Group or a Person
|
86
|
+
|
87
|
+
belongs_to :group
|
88
|
+
|
89
|
+
uuid_lsn 0xf
|
90
|
+
end
|
91
|
+
```
|
74
92
|
|
75
93
|
Copyright (c) 2011 PeepAll Ltd, released under the MIT license
|
94
|
+
|
95
|
+
|
96
|
+
[1]: http://mongomapper.com/documentation/plugins/single-collection-inheritance.html
|
data/lib/mm_uses_uuid/version.rb
CHANGED
data/lib/mm_uses_uuid.rb
CHANGED
@@ -19,11 +19,26 @@ class BsonUuid
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
class MongoMapper::Plugins::Associations::InArrayProxy
|
23
|
+
|
24
|
+
def find_target
|
25
|
+
return [] if ids.blank?
|
26
|
+
if klass == UuidModel
|
27
|
+
UuidModel.find(*criteria[:_id])
|
28
|
+
else
|
29
|
+
all
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
class UuidModel
|
36
|
+
|
37
|
+
include MongoMapper::Document
|
23
38
|
|
24
39
|
@@lsn_class ||= []
|
25
40
|
|
26
|
-
def self.
|
41
|
+
def self.find(*args)
|
27
42
|
args.flatten!
|
28
43
|
ids_by_class = {}
|
29
44
|
args.each do |id|
|
@@ -35,7 +50,7 @@ module MongoMapper
|
|
35
50
|
end
|
36
51
|
ids_by_class.map {|klass, ids| klass.find(ids)} .flatten
|
37
52
|
end
|
38
|
-
|
53
|
+
|
39
54
|
end
|
40
55
|
|
41
56
|
module MmUsesUuid
|
@@ -75,7 +90,7 @@ module MmUsesUuid
|
|
75
90
|
end
|
76
91
|
|
77
92
|
def add_class_lsn(klass, lsn_integer)
|
78
|
-
|
93
|
+
UuidModel.class_eval "@@lsn_class[#{lsn_integer}] = #{klass}"
|
79
94
|
end
|
80
95
|
|
81
96
|
end
|
@@ -111,7 +126,7 @@ module MmUsesUuid
|
|
111
126
|
|
112
127
|
def make_uuid
|
113
128
|
uuid = SecureRandom.uuid.gsub!('-', '')
|
114
|
-
lsn_class =
|
129
|
+
lsn_class = UuidModel.class_variable_get('@@lsn_class')
|
115
130
|
if replacement_lsn = lsn_class.index(self.class)
|
116
131
|
uuid[-1] = replacement_lsn.to_s(16)
|
117
132
|
end
|
data/spec/mm_uses_uuid_spec.rb
CHANGED
@@ -21,6 +21,9 @@ describe MmUsesUuid do
|
|
21
21
|
key :name
|
22
22
|
key :age
|
23
23
|
|
24
|
+
key :interest_ids, Array
|
25
|
+
many :interests, :in => :interest_ids, :class_name => 'UuidModel' # this allows many-to-many polymorphic interests without the need for groups and people to be stored in a single collection
|
26
|
+
|
24
27
|
belongs_to :group
|
25
28
|
|
26
29
|
uuid_lsn 0xf
|
@@ -43,7 +46,11 @@ describe MmUsesUuid do
|
|
43
46
|
"33333333-3333-4333-y333-333333333333"
|
44
47
|
)
|
45
48
|
@group = Group.create(name: 'mongo_mapper fanclub')
|
46
|
-
|
49
|
+
|
50
|
+
@person = Person.new(name: 'Jon', age: 33)
|
51
|
+
@person.interests << @group
|
52
|
+
@person.interests << @person #I'm very self-centred
|
53
|
+
@person.save
|
47
54
|
end
|
48
55
|
|
49
56
|
it "should cause newly initialized objects to have a BSON::Binary uuid" do
|
@@ -57,7 +64,12 @@ describe MmUsesUuid do
|
|
57
64
|
end
|
58
65
|
|
59
66
|
it "should perform a find on the right collection if MongoMapper.find is used" do
|
60
|
-
|
67
|
+
UuidModel.find(@person.id, @group.id).map(&:name).should include(@person.name, @group.name)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should support polymorphic many to many associations that use LSN encoding" do
|
71
|
+
person = Person.find_by_name 'Jon'
|
72
|
+
person.interests.map(&:name).should include(@person.name, @group.name)
|
61
73
|
end
|
62
74
|
|
63
75
|
it "should not set a new uuid if one as passed as a param" do
|